Ejemplo n.º 1
0
        public override Property ReadJson(JsonReader reader, Type objectType, Property existingValue, bool hasExistingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null)
            {
                return(null);
            }

            IReferenceResolver resolver = serializer.Context.Context as IReferenceResolver;

            Property target = null;
            string   id     = string.Empty;

            JObject json = JObject.Load(reader);

            foreach (JProperty property in json.Properties())
            {
                if (property.Name == "$ref")
                {
                    target = (Property)serializer.Deserialize(json.CreateReader());
                }
                else if (property.Name == "$id")
                {
                    id = (string)property.Value;
                }
                else if (property.Name == "identity")
                {
                    Guid identity = new Guid((string)property.Value);
                    IReferenceObjectFactory factory = MetadataPersistentContext.Current.Factory;
                    target = factory.New <Property>(identity);
                    resolver.AddReference(null, id, target);
                }
            }
            return(target);
        }
        private void DeserializeBooleanOperator(JsonSerializer serializer, JObject json, BooleanOperator target)
        {
            IReferenceResolver resolver = serializer.Context.Context as IReferenceResolver;

            foreach (JProperty property in json.Properties())
            {
                if (property.Name == "$id")
                {
                    string id = (string)serializer.Deserialize(property.Value.CreateReader());
                    resolver.AddReference(null, id, target);
                }
                else if (property.Name == "Consumer")
                {
                    target.Consumer = (HermesModel)serializer.Deserialize(property.Value.CreateReader());
                }
                else if (property.Name == "Name")
                {
                    target.Name = (string)property.Value;
                }
                else if (property.Name == "Operands")
                {
                    target.Operands = serializer.Deserialize <List <BooleanFunction> >(property.Value.CreateReader());
                }
            }
        }
        public override PropertyReference ReadJson(JsonReader reader, Type objectType, PropertyReference existingValue, bool hasExistingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null)
            {
                return(null);
            }

            IReferenceResolver resolver = serializer.Context.Context as IReferenceResolver;

            PropertyReference target = new PropertyReference(null);

            JObject json = JObject.Load(reader);

            foreach (JProperty property in json.Properties())
            {
                if (property.Name == "$id")
                {
                    string id = (string)serializer.Deserialize(property.Value.CreateReader());
                    resolver.AddReference(null, id, target);
                }
                else if (property.Name == "Consumer")
                {
                    target.Consumer = (HermesModel)serializer.Deserialize(property.Value.CreateReader());
                }
                else if (property.Name == "Table")
                {
                    JObject   expression  = JObject.Load(property.Value.CreateReader());
                    JProperty refProperty = expression.Properties().Where(p => p.Name == "$ref").FirstOrDefault();
                    if (refProperty != null)
                    {
                        target.Table = (TableExpression)serializer.Deserialize(property.Value.CreateReader());
                    }
                    else
                    {
                        JProperty typeProperty = expression.Properties().Where(p => p.Name == "$type").FirstOrDefault();
                        if (typeProperty == null)
                        {
                            target.Table = null;
                        }
                        else
                        {
                            string typeName = (string)serializer.Deserialize(typeProperty.Value.CreateReader());
                            Type   type     = serializer.SerializationBinder.BindToType(null, typeName);
                            target.Table = (TableExpression)serializer.Deserialize(property.Value.CreateReader(), type);
                        }
                    }
                }
                else if (property.Name == "Property")
                {
                    target.Property = serializer.Deserialize <Property>(property.Value.CreateReader());
                }
            }

            return(target);
        }
Ejemplo n.º 4
0
        public override SelectStatement ReadJson(JsonReader reader, Type objectType, SelectStatement existingValue, bool hasExistingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null)
            {
                return(null);
            }

            IReferenceResolver resolver = serializer.Context.Context as IReferenceResolver;

            SelectStatement target = new SelectStatement(null, null);

            JObject json = JObject.Load(reader);

            foreach (JProperty property in json.Properties())
            {
                if (property.Name == "$id")
                {
                    string id = (string)serializer.Deserialize(property.Value.CreateReader());
                    resolver.AddReference(null, id, target);
                }
                else if (property.Name == "Consumer")
                {
                    target.Consumer = (HermesModel)serializer.Deserialize(property.Value.CreateReader());
                }
                else if (property.Name == "Alias")
                {
                    target.Alias = (string)serializer.Deserialize(property.Value.CreateReader());
                }
                else if (property.Name == "Hint")
                {
                    target.Hint = (string)serializer.Deserialize(property.Value.CreateReader());
                }
                else if (property.Name == "FROM")
                {
                    DeserializeFROM(serializer, property, target);
                }
                else if (property.Name == "SELECT")
                {
                    DeserializeSELECT(serializer, property, target);
                }
                else if (property.Name == "WHERE")
                {
                    DeserializeWHERE(serializer, property, target);
                }
            }

            return(target);
        }
Ejemplo n.º 5
0
        public override JoinExpression ReadJson(JsonReader reader, Type objectType, JoinExpression existingValue, bool hasExistingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null)
            {
                return(null);
            }

            IReferenceResolver resolver = serializer.Context.Context as IReferenceResolver;

            JoinExpression target = new JoinExpression(null, null);

            JObject json = JObject.Load(reader);

            foreach (JProperty property in json.Properties())
            {
                if (property.Name == "$id")
                {
                    string id = (string)serializer.Deserialize(property.Value.CreateReader());
                    resolver.AddReference(null, id, target);
                }
                else if (property.Name == "Consumer")
                {
                    target.Consumer = (HermesModel)serializer.Deserialize(property.Value.CreateReader());
                }
                else if (property.Name == "Alias")
                {
                    target.Alias = (string)serializer.Deserialize(property.Value.CreateReader());
                }
                else if (property.Name == "Hint")
                {
                    target.Hint = (string)serializer.Deserialize(property.Value.CreateReader());
                }
                else if (property.Name == "Entity")
                {
                    target.Entity = serializer.Deserialize <Entity>(property.Value.CreateReader());
                }
                else if (property.Name == "JoinType")
                {
                    target.JoinType = (string)serializer.Deserialize(property.Value.CreateReader());
                }
                else if (property.Name == "ON")
                {
                    target.ON = serializer.Deserialize <BooleanFunction>(property.Value.CreateReader());
                }
            }

            return(target);
        }
        public override ParameterExpression ReadJson(JsonReader reader, Type objectType, ParameterExpression existingValue, bool hasExistingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null)
            {
                return(null);
            }

            IReferenceResolver resolver = serializer.Context.Context as IReferenceResolver;

            ParameterExpression target = new ParameterExpression(null);

            JObject json = JObject.Load(reader);

            foreach (JProperty property in json.Properties())
            {
                if (property.Name == "$id")
                {
                    string id = (string)serializer.Deserialize(property.Value.CreateReader());
                    resolver.AddReference(null, id, target);
                }
                else if (property.Name == "Consumer")
                {
                    target.Consumer = (HermesModel)serializer.Deserialize(property.Value.CreateReader());
                }
                else if (property.Name == "Name")
                {
                    target.Name = (string)property.Value;
                }
                else if (property.Name == "Type")
                {
                    target.Type = serializer.Deserialize <Entity>(property.Value.CreateReader());
                }
                else if (property.Name == "Value")
                {
                    if (property.Value.Type == JTokenType.Object)
                    {
                        target.Value = serializer.Deserialize <ReferenceProxy>(property.Value.CreateReader());
                    }
                    else
                    {
                        target.Value = serializer.Deserialize(property.Value.CreateReader());
                    }
                }
            }

            return(target);
        }
Ejemplo n.º 7
0
        public override ReferenceProxy ReadJson(JsonReader reader, Type objectType, ReferenceProxy existingValue, bool hasExistingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null)
            {
                return(null);
            }

            IReferenceResolver resolver = serializer.Context.Context as IReferenceResolver;

            ReferenceProxy target = null;
            string         id     = string.Empty;
            Entity         entity = null;

            JObject json = JObject.Load(reader);

            foreach (JProperty property in json.Properties())
            {
                if (property.Name == "$ref")
                {
                    target = (ReferenceProxy)serializer.Deserialize(json.CreateReader());
                }
                else if (property.Name == "$id")
                {
                    id = (string)property.Value;
                }
                else if (property.Name == "type")
                {
                    entity = serializer.Deserialize <Entity>(property.Value.CreateReader());
                }
                else if (property.Name == "identity")
                {
                    Guid identity = new Guid((string)property.Value);
                    target = new ReferenceProxy(entity, identity);
                    resolver.AddReference(null, id, target);
                }
            }
            return(target);
        }
    public void AddReference(object context, string reference, object value)
    {
        var representative = GetOrAddRepresentativeObject(value);

        defaultResolver.AddReference(context, reference, representative);
    }
        private DummyAssembly ReadObject(ref Utf8JsonReader reader, JsonSerializerOptions options)
        {
            byte[] ID   = Encoding.UTF8.GetBytes("$id");
            byte[] REF  = Encoding.UTF8.GetBytes("$ref");
            byte[] TYPE = Encoding.UTF8.GetBytes("$type");

            string       reference1   = string.Empty;
            string       propertyName = string.Empty;
            PropertyInfo propertyInfo = null;

            DummyAssembly entity     = null;
            Type          entityType = null;

            while (reader.Read())
            {
                if (reader.TokenType == JsonTokenType.StartObject)
                {
                    if (propertyInfo.IsOptional())
                    {
                        IOptional optional = (IOptional)propertyInfo.GetValue(entity);
                        ReadOption(ref reader, optional, options);
                    }
                    else
                    {
                        DummyAssembly value = ReadObject(ref reader, options);
                        propertyInfo.SetValue(entity, value);
                    }
                }
                else if (reader.TokenType == JsonTokenType.EndObject)
                {
                    return(entity);
                }
                else if (reader.TokenType == JsonTokenType.PropertyName)
                {
                    if (reader.ValueTextEquals(REF))
                    {
                        reader.Read();
                        string referenceN = reader.GetString();
                        entity = (DummyAssembly)_resolver.ResolveReference(referenceN);
                        while (reader.TokenType != JsonTokenType.EndObject)
                        {
                            if (!reader.Read())
                            {
                                break;
                            }
                        }
                        return(entity);
                    }
                    else if (reader.ValueTextEquals(ID))
                    {
                        reader.Read();
                        reference1 = reader.GetString();
                    }
                    else if (reader.ValueTextEquals(TYPE))
                    {
                        reader.Read();
                        string typeCode = reader.GetString();
                        entityType = _binder.GetType(typeCode);
                        entity     = (DummyAssembly)Activator.CreateInstance(entityType);
                        _resolver.AddReference(reference1, entity);
                    }
                    else
                    {
                        propertyName = reader.GetString();
                        propertyInfo = entityType.GetProperty(propertyName);
                    }
                }
                else if (reader.TokenType == JsonTokenType.Null)
                {
                    propertyInfo.SetValue(entity, null);
                }
                else if (reader.TokenType == JsonTokenType.True)
                {
                    propertyInfo.SetValue(entity, true);
                }
                else if (reader.TokenType == JsonTokenType.False)
                {
                    propertyInfo.SetValue(entity, false);
                }
                else if (reader.TokenType == JsonTokenType.Number)
                {
                    uint intValue = reader.GetUInt32();
                    if (propertyInfo.PropertyType.IsEnum)
                    {
                        propertyInfo.SetValue(entity, Enum.GetValues(propertyInfo.PropertyType).GetValue(intValue));
                    }
                    else
                    {
                        propertyInfo.SetValue(entity, intValue);
                    }
                }
                else if (reader.TokenType == JsonTokenType.String)
                {
                    string stringValue = reader.GetString();
                    if (propertyInfo.PropertyType == typeof(Type))
                    {
                        propertyInfo.SetValue(entity, Type.GetType(stringValue));
                    }
                    else if (propertyInfo.PropertyType == typeof(Assembly))
                    {
                        //AppDomain.CurrentDomain.GetAssemblies().SingleOrDefault(assembly => assembly.GetName().Name == name);
                        propertyInfo.SetValue(entity, Assembly.Load(stringValue));
                    }
                    else
                    {
                        propertyInfo.SetValue(entity, stringValue);
                    }
                }
                else if (reader.TokenType == JsonTokenType.StartArray)
                {
                    IList list = (IList)propertyInfo.GetValue(entity);
                    while (reader.TokenType != JsonTokenType.EndArray)
                    {
                        if (!reader.Read() || reader.TokenType == JsonTokenType.EndArray)
                        {
                            break;
                        }
                        DummyAssembly item = ReadObject(ref reader, options);
                        list.Add(item);
                    }
                }
            }
            return(entity); // never gets here - JsonTokenType.EndObject returns ...
        }
        private void DeserializeComparisonOperator(JsonSerializer serializer, JObject json, ComparisonOperator target)
        {
            IReferenceResolver resolver = serializer.Context.Context as IReferenceResolver;

            foreach (JProperty property in json.Properties())
            {
                if (property.Name == "$id")
                {
                    string id = (string)serializer.Deserialize(property.Value.CreateReader());
                    resolver.AddReference(null, id, target);
                }
                else if (property.Name == "Consumer")
                {
                    target.Consumer = (HermesModel)serializer.Deserialize(property.Value.CreateReader());
                }
                else if (property.Name == "Name")
                {
                    target.Name = (string)property.Value;
                }
                else if (property.Name == "LeftExpression")
                {
                    JObject   expression  = JObject.Load(property.Value.CreateReader());
                    JProperty refProperty = expression.Properties().Where(p => p.Name == "$ref").FirstOrDefault();
                    if (refProperty != null)
                    {
                        target.LeftExpression = (HermesModel)serializer.Deserialize(property.Value.CreateReader());
                    }
                    else
                    {
                        JProperty typeProperty = expression.Properties().Where(p => p.Name == "$type").FirstOrDefault();
                        if (typeProperty == null)
                        {
                            target.LeftExpression = null;
                        }
                        else
                        {
                            string typeName = (string)serializer.Deserialize(typeProperty.Value.CreateReader());
                            Type   type     = serializer.SerializationBinder.BindToType(null, typeName);
                            target.LeftExpression = (HermesModel)serializer.Deserialize(property.Value.CreateReader(), type);
                        }
                    }
                }
                else if (property.Name == "RightExpression")
                {
                    JObject   expression  = JObject.Load(property.Value.CreateReader());
                    JProperty refProperty = expression.Properties().Where(p => p.Name == "$ref").FirstOrDefault();
                    if (refProperty != null)
                    {
                        target.RightExpression = (HermesModel)serializer.Deserialize(property.Value.CreateReader());
                    }
                    else
                    {
                        JProperty typeProperty = expression.Properties().Where(p => p.Name == "$type").FirstOrDefault();
                        if (typeProperty == null)
                        {
                            target.RightExpression = null;
                        }
                        else
                        {
                            string typeName = (string)serializer.Deserialize(typeProperty.Value.CreateReader());
                            Type   type     = serializer.SerializationBinder.BindToType(null, typeName);
                            target.RightExpression = (HermesModel)serializer.Deserialize(property.Value.CreateReader(), type);
                        }
                    }
                }
            }
        }
Ejemplo n.º 11
0
        private Entity ReadObject(ref Utf8JsonReader reader, JsonSerializerOptions options)
        {
            byte[] ID   = Encoding.UTF8.GetBytes("$id");
            byte[] REF  = Encoding.UTF8.GetBytes("$ref");
            byte[] TYPE = Encoding.UTF8.GetBytes("$type");

            string       reference1   = string.Empty;
            string       propertyName = string.Empty;
            PropertyInfo propertyInfo = null;

            Entity entity     = null;
            Type   entityType = null;

            while (reader.Read())
            {
                if (reader.TokenType == JsonTokenType.StartObject)
                {
                    Entity value = ReadObject(ref reader, options);
                    propertyInfo.SetValue(entity, value);
                }
                else if (reader.TokenType == JsonTokenType.EndObject)
                {
                    return(entity);
                }
                else if (reader.TokenType == JsonTokenType.PropertyName)
                {
                    if (reader.ValueTextEquals(REF))
                    {
                        reader.Read();
                        string referenceN = reader.GetString();
                        entity = (Entity)_resolver.ResolveReference(referenceN);
                        while (reader.TokenType != JsonTokenType.EndObject)
                        {
                            if (!reader.Read())
                            {
                                break;
                            }
                        }
                        return(entity);
                    }
                    else if (reader.ValueTextEquals(ID))
                    {
                        reader.Read();
                        reference1 = reader.GetString();
                    }
                    else if (reader.ValueTextEquals(TYPE))
                    {
                        reader.Read();
                        int typeCode = reader.GetInt32();
                        entityType = _binder.GetType(typeCode);
                        entity     = (Entity)Activator.CreateInstance(entityType);
                        _resolver.AddReference(reference1, entity);
                    }
                    else
                    {
                        propertyName = reader.GetString();
                        propertyInfo = entityType.GetProperty(propertyName);
                    }
                }
                else if (reader.TokenType == JsonTokenType.Null)
                {
                    propertyInfo.SetValue(entity, null);
                }
                else if (reader.TokenType == JsonTokenType.True)
                {
                    propertyInfo.SetValue(entity, true);
                }
                else if (reader.TokenType == JsonTokenType.False)
                {
                    propertyInfo.SetValue(entity, false);
                }
                else if (reader.TokenType == JsonTokenType.Number)
                {
                    propertyInfo.SetValue(entity, reader.GetUInt32());
                }
                else if (reader.TokenType == JsonTokenType.String)
                {
                    propertyInfo.SetValue(entity, reader.GetString());
                }
                else if (reader.TokenType == JsonTokenType.StartArray)
                {
                    IList list = (IList)propertyInfo.GetValue(entity);
                    while (reader.TokenType != JsonTokenType.EndArray)
                    {
                        if (!reader.Read() || reader.TokenType == JsonTokenType.EndArray)
                        {
                            break;
                        }
                        Entity item = ReadObject(ref reader, options);
                        list.Add(item);
                    }
                }
            }
            return(entity);
        }
        public override QueryExpression ReadJson(JsonReader reader, Type objectType, QueryExpression existingValue, bool hasExistingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null)
            {
                return(null);
            }

            IReferenceResolver resolver = serializer.Context.Context as IReferenceResolver;

            QueryExpression target = new QueryExpression();

            JObject json = JObject.Load(reader);

            foreach (JProperty property in json.Properties())
            {
                if (property.Name == "$id")
                {
                    string id = (string)serializer.Deserialize(property.Value.CreateReader());
                    resolver.AddReference(null, id, target);
                }
                else if (property.Name == "Parameters")
                {
                    JArray array = (JArray)serializer.Deserialize(property.Value.CreateReader());
                    if (array == null)
                    {
                        target.Parameters = null;
                    }
                    else if (array.Count == 0)
                    {
                        target.Parameters = new List <ParameterExpression>();
                    }
                    else
                    {
                        target.Parameters = new List <ParameterExpression>();
                        foreach (JObject item in array)
                        {
                            target.Parameters.Add(serializer.Deserialize <ParameterExpression>(item.CreateReader()));
                        }
                    }
                }
                else if (property.Name == "Expressions")
                {
                    JArray array = (JArray)serializer.Deserialize(property.Value.CreateReader());
                    if (array == null)
                    {
                        target.Expressions = null;
                    }
                    else if (array.Count == 0)
                    {
                        target.Expressions = new List <HermesModel>();
                    }
                    else
                    {
                        target.Expressions = new List <HermesModel>();
                        foreach (JObject item in array)
                        {
                            string typeName = (string)serializer.Deserialize(item["$type"].CreateReader());
                            //string typeName = (string)serializer.Deserialize(item.Properties().Where(p => p.Name == "$type").FirstOrDefault().Value.CreateReader());
                            Type type = serializer.SerializationBinder.BindToType(null, typeName);
                            target.Expressions.Add((HermesModel)serializer.Deserialize(item.CreateReader(), type));
                        }
                    }
                }
            }

            return(target);
        }