Exemplo n.º 1
0
        private Boolean TryConvertToLiteralToken(
            TemplateContext context,
            EvaluationResult result,
            out LiteralToken literal)
        {
            if (result.Raw is LiteralToken literal2)
            {
                context.Memory.AddBytes(literal2);
                literal = literal2;
                return(true);
            }

            switch (result.Kind)
            {
            case ValueKind.Null:
                literal = new NullToken(FileId, Line, Column);
                break;

            case ValueKind.Boolean:
                literal = new BooleanToken(FileId, Line, Column, (Boolean)result.Value);
                break;

            case ValueKind.Number:
                literal = new NumberToken(FileId, Line, Column, (Double)result.Value);
                break;

            case ValueKind.String:
                literal = new StringToken(FileId, Line, Column, (String)result.Value);
                break;

            default:
                literal = null;
                return(false);
            }

            context.Memory.AddBytes(literal);
            return(true);
        }
        public override Object ReadJson(
            JsonReader reader,
            Type objectType,
            Object existingValue,
            JsonSerializer serializer)
        {
            switch (reader.TokenType)
            {
            case JsonToken.String:
                return(new StringToken(null, null, null, reader.Value.ToString()));

            case JsonToken.Boolean:
                return(new BooleanToken(null, null, null, (Boolean)reader.Value));

            case JsonToken.Float:
                return(new NumberToken(null, null, null, (Double)reader.Value));

            case JsonToken.Integer:
                return(new NumberToken(null, null, null, (Double)(Int64)reader.Value));

            case JsonToken.Null:
                return(new NullToken(null, null, null));

            case JsonToken.StartObject:
                break;

            default:
                return(null);
            }

            Int32?  type  = null;
            JObject value = JObject.Load(reader);

            if (!value.TryGetValue("type", StringComparison.OrdinalIgnoreCase, out JToken typeValue))
            {
                type = TokenType.String;
            }
            else if (typeValue.Type == JTokenType.Integer)
            {
                type = (Int32)typeValue;
            }
            else
            {
                return(existingValue);
            }

            Object newValue = null;

            switch (type)
            {
            case TokenType.Null:
                newValue = new NullToken(null, null, null);
                break;

            case TokenType.Boolean:
                newValue = new BooleanToken(null, null, null, default(Boolean));
                break;

            case TokenType.Number:
                newValue = new NumberToken(null, null, null, default(Double));
                break;

            case TokenType.String:
                newValue = new StringToken(null, null, null, null);
                break;

            case TokenType.BasicExpression:
                newValue = new BasicExpressionToken(null, null, null, null);
                break;

            case TokenType.InsertExpression:
                newValue = new InsertExpressionToken(null, null, null);
                break;

            case TokenType.Sequence:
                newValue = new SequenceToken(null, null, null);
                break;

            case TokenType.Mapping:
                newValue = new MappingToken(null, null, null);
                break;
            }

            if (value != null)
            {
                using (JsonReader objectReader = value.CreateReader())
                {
                    serializer.Populate(objectReader, newValue);
                }
            }

            return(newValue);
        }