Beispiel #1
0
        private void ReadBase(object obj, Dictionary <string, object> fields, ref JsonOutputObject outputJsonObject)
        {
            Type objType = Type.GetType(fields[fieldType].ToString());

            foreach (string fieldName in fields.Keys)
            {
                if (!IsSpecialField(fieldName))
                {
                    object    value = fields[fieldName];
                    FieldInfo field = objType.GetField(fieldName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                    if (IsRef(value))
                    {
                        LinkToField newLink = new LinkToField();
                        newLink.Obj      = obj;
                        newLink.Field    = field;
                        newLink.RefIndex = ParseRef(value.ToString());
                        outputJsonObject.Links.Add(newLink);
                    }
                    else
                    {
                        field.SetValue(obj, ConvertValue(value, field.FieldType));
                    }
                }
            }
        }
Beispiel #2
0
        private Dictionary <string, object> Read(JsonTextReader reader, ref JsonOutputObject outputJsonObject)
        {
            Dictionary <string, object> fields = new Dictionary <string, object>();
            string key = null;

            while (reader.Read() && reader.TokenType != JsonToken.EndObject)
            {
                if (reader.TokenType == JsonToken.PropertyName)
                {
                    key = reader.Value.ToString();
                    if (key == fieldArraySize && reader.Read())
                    {
                        List <int> list = new List <int>();
                        if (reader.TokenType == JsonToken.StartArray)
                        {
                            while (reader.Read() && reader.TokenType != JsonToken.EndArray)
                            {
                                list.Add((int)(long)reader.Value);
                            }
                        }
                        else
                        {
                            list.Add((int)(long)reader.Value);
                        }

                        fields.Add(key, list);
                    }
                }
                else if (reader.TokenType == JsonToken.StartObject)
                {
                    if (key == fieldBase)
                    {
                        fields.Add(key, Read(reader, ref outputJsonObject));
                    }
                    else
                    {
                        JsonOutputObject newObj = ReadJsonObject(reader);
                        outputJsonObject.Links.AddRange(newObj.Links);
                        fields.Add(key, newObj.Object);
                    }
                }
                else if (reader.TokenType == JsonToken.StartArray)
                {
                    List <JsonArrayElement> list = new List <JsonArrayElement>();
                    ReadArray(reader, list, ref outputJsonObject);
                    fields.Add(key, list);
                }
                else if (reader.Value != null)
                {
                    fields.Add(key, reader.Value);
                }
            }

            return(fields);
        }
Beispiel #3
0
        private void CreateObject(Dictionary <string, object> fields, ref JsonOutputObject outputJsonObject)
        {
            if (!fields.ContainsKey(fieldType))
            {
                return;
            }

            if (fields.ContainsKey(fieldRef))
            {
                outputJsonObject.RefIndex = ToRefIndex(fields[fieldRef]);
            }
            else
            {
                outputJsonObject.RefIndex = 0;
            }

            Type objType = Type.GetType(fields[fieldType].ToString());

            if (objType == null)
            {
                return;
            }

            object obj = Activator.CreateInstance(objType);

            foreach (string fieldName in fields.Keys)
            {
                if (!IsSpecialField(fieldName))
                {
                    object    value = fields[fieldName];
                    FieldInfo field = objType.GetField(fieldName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                    if (IsRef(value))
                    {
                        LinkToField newLink = new LinkToField();
                        newLink.Obj      = obj;
                        newLink.Field    = field;
                        newLink.RefIndex = ParseRef(value.ToString());
                        outputJsonObject.Links.Add(newLink);
                    }
                    else
                    {
                        field.SetValue(obj, ConvertValue(value, field.FieldType));
                    }
                }
            }

            if (fields.ContainsKey(fieldBase))
            {
                ReadBase(obj, fields[fieldBase] as Dictionary <string, object>, ref outputJsonObject);
            }

            outputJsonObject.Object = obj;
        }
Beispiel #4
0
        private void CreateArray(Dictionary <string, object> fields, ref JsonOutputObject outputJsonObject)
        {
            Type objType = Type.GetType(fields[fieldType].ToString());

            if (objType == null)
            {
                return;
            }

            if (fields.ContainsKey(fieldRef))
            {
                outputJsonObject.RefIndex = ToRefIndex(fields[fieldRef]);
            }
            else
            {
                outputJsonObject.RefIndex = 0;
            }

            List <int> Length = fields[fieldArraySize] as List <int>;
            List <JsonArrayElement> values = fields[fieldArrayElements] as List <JsonArrayElement>;

            Type[]   sizeTypes = new Type[Length.Count];
            object[] sizes     = new object[Length.Count];
            for (int i = 0; i < sizeTypes.Length; i++)
            {
                sizeTypes[i] = typeof(Int32);
                sizes[i]     = Length[i];
            }

            ConstructorInfo constructor = objType.GetConstructor(sizeTypes);
            Array           arr         = constructor.Invoke(sizes) as Array;

            foreach (JsonArrayElement value in values)
            {
                if (IsRef(value.Value))
                {
                    LinkToArrayEl newLink = new LinkToArrayEl();
                    newLink.Obj      = arr;
                    newLink.Indexes  = value.Indexes;
                    newLink.RefIndex = ParseRef(value.Value.ToString());
                    outputJsonObject.Links.Add(newLink);
                }
                else
                {
                    arr.SetValue(value.Value, value.Indexes);
                }
            }

            outputJsonObject.Object = arr;
        }
Beispiel #5
0
 private void ReadArray(JsonTextReader reader, List <JsonArrayElement> arr, ref JsonOutputObject outputJsonObject)
 {
     while (reader.Read() && reader.TokenType != JsonToken.EndArray)
     {
         if (reader.TokenType == JsonToken.StartObject)
         {
             JsonArrayElement el = new JsonArrayElement();
             while (reader.Read() && reader.TokenType != JsonToken.EndObject)
             {
                 if (reader.TokenType == JsonToken.PropertyName)
                 {
                     if (reader.Value.Equals(fieldArrayElIndex) && reader.Read())
                     {
                         List <int> indexesList = new List <int>();
                         if (reader.TokenType == JsonToken.StartArray)
                         {
                             while (reader.Read() && reader.TokenType != JsonToken.EndArray)
                             {
                                 indexesList.Add((int)(long)reader.Value);
                             }
                         }
                         else
                         {
                             indexesList.Add((int)(long)reader.Value);
                         }
                         el.Indexes = indexesList.ToArray();
                     }
                     else if (reader.Value.Equals(fieldArrayElValue) && reader.Read())
                     {
                         if (reader.TokenType == JsonToken.StartObject)
                         {
                             JsonOutputObject newObj = ReadJsonObject(reader);
                             outputJsonObject.Links.AddRange(newObj.Links);
                             el.Value = newObj.Object;
                         }
                         else
                         {
                             el.Value = reader.Value;
                         }
                     }
                 }
             }
             arr.Add(el);
         }
         else if (reader.TokenType == JsonToken.StartArray)
         {
             ReadArray(reader, arr, ref outputJsonObject);
         }
     }
 }
Beispiel #6
0
        private JsonOutputObject ReadJsonObject(JsonTextReader reader)
        {
            JsonOutputObject outputJsonObject = new JsonOutputObject();

            Dictionary <string, object> fields = Read(reader, ref outputJsonObject);

            string n       = fields[fieldType].ToString();
            Type   objType = Type.GetType(n);

            if (objType.IsArray)
            {
                CreateArray(fields, ref outputJsonObject);
            }
            else
            {
                CreateObject(fields, ref outputJsonObject);
            }

            return(outputJsonObject);
        }
Beispiel #7
0
        public object Deserialize(Stream serializationStream)
        {
            ObjectsBufferLoad objectsBuffer = new ObjectsBufferLoad();
            JsonTextReader    reader        = new JsonTextReader(new StreamReader(serializationStream));

            try {
                while (reader.Read() && reader.TokenType != JsonToken.EndObject)
                {
                    if (reader.TokenType == JsonToken.PropertyName)
                    {
                        if (reader.Value.Equals(fieldRoot) && reader.Read())
                        {
                            objectsBuffer.RootRef = ToRefIndex(reader.Value);
                        }
                        else if (reader.Value.Equals(fieldArrayElements) && reader.Read())
                        {
                            while (reader.Read() && reader.TokenType != JsonToken.EndArray)
                            {
                                if (reader.TokenType == JsonToken.StartObject)
                                {
                                    JsonOutputObject outputObj = ReadJsonObject(reader);
                                    objectsBuffer.Objects.Add(outputObj.RefIndex, outputObj.Object);
                                    foreach (Link link in outputObj.Links)
                                    {
                                        objectsBuffer.Links.Add(link);
                                    }
                                }
                            }
                        }
                    }
                }

                foreach (Link link in objectsBuffer.Links)
                {
                    object value;
                    if (link is LinkToField)
                    {
                        LinkToField linkToField = link as LinkToField;
                        if (objectsBuffer.Objects.TryGetValue(linkToField.RefIndex, out value))
                        {
                            try {
                                linkToField.Field.SetValue(link.Obj, value);
                            }
                            catch (Exception e) {
                            }
                        }
                    }
                    else if (link is LinkToArrayEl)
                    {
                        LinkToArrayEl linkToArrayEl = link as LinkToArrayEl;
                        Array         arr           = linkToArrayEl.Obj as Array;
                        if (objectsBuffer.Objects.TryGetValue(link.RefIndex, out value))
                        {
                            try {
                                arr.SetValue(value, linkToArrayEl.Indexes);
                            }
                            catch (Exception e) {
                            }
                        }
                    }
                }

                return(objectsBuffer.RootObject);
            } catch (Exception) {
                return(null);
            }
        }