Example #1
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;
        }
Example #2
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);
            }
        }