public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            //we may be starting the deserialization here, if thats the case we need to resolve this object as the root
            object obj;

            if (DocumentRootConverter.TryResolveAsRootData(reader, objectType, serializer, out obj))
            {
                return(obj);
            }

            JsonObjectContract contract = (JsonObjectContract)serializer.ContractResolver.ResolveContract(objectType);
            var isNew = false;

            if (existingValue == null)
            {
                existingValue = contract.DefaultCreator();
                isNew         = true;
            }

            existingValue = PopulateProperties(serializer, existingValue, reader, contract);
            if (isNew)
            {
                var id       = contract.Properties.GetClosestMatchProperty(PropertyNames.Id).ValueProvider.GetValue(existingValue);
                var type     = existingValue.GetType().Name.ToLower();
                var typeProp = contract.Properties.GetClosestMatchProperty(PropertyNames.Type);
                if (typeProp != null)
                {
                    type = typeProp.ValueProvider.GetValue(existingValue).ToString();
                }

                serializer.ReferenceResolver.AddReference(null, type + ":" + id, existingValue);
            }
            return(existingValue);
        }
예제 #2
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            object list;

            if (DocumentRootConverter.TryResolveAsRootData(reader, objectType, serializer, out list))
            {
                return(list);
            }

            ////read into the 'Data' path
            //var preDataPath = ReaderUtil.ReadUntilStart(reader, DataPathRegex);

            //we should be dealing with list types, but we also want the element type
            Type elementType;

            if (!ListUtil.IsList(objectType, out elementType))
            {
                throw new ArgumentException($"{typeof(ResourceListConverter)} can only read json lists", nameof(objectType));
            }

            var itemsIterator = ReaderUtil.IterateList(reader).Select(x => serializer.Deserialize(reader, elementType));

            list = ListUtil.CreateList(objectType, itemsIterator);

            ////read out of the 'Data' path
            //ReaderUtil.ReadUntilEnd(reader, preDataPath);

            return(list);
        }
예제 #3
0
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            if (DocumentRootConverter.TryResolveAsRootData(writer, value, serializer))
            {
                return;
            }

            WriterUtil.WriteIntoElement(writer, DataPathRegex, PropertyNames.Data, () =>
            {
                var enumerable = value as IEnumerable <object> ?? Enumerable.Empty <object>();
                writer.WriteStartArray();
                foreach (var valueElement in enumerable)
                {
                    serializer.Serialize(writer, valueElement);
                }
                writer.WriteEndArray();
            });
        }
 public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
 {
     //Check Document Root is exits
     if (DocumentRootConverter.TryResolveAsRootData(writer, value, serializer))
     {
         return;
     }
     WriterUtil.WriteIntoElement(writer, DataWritePathRegex, PropertyNames.Data, () =>
     {
         var probe = writer as AttributeOrRelationshipProbe;
         if (probe != null)
         {
             //if someone is sending a probe its because we are in a relationship property.
             //let the probe know we are in a relationship and write the reference element
             probe.PropertyType = AttributeOrRelationshipProbe.Type.Relationship;
             WriteRelationshipJson(writer, value, serializer);
         }
         else
         {
             WriteResourceJson(writer, value, serializer);
         }
     });
 }