Ejemplo n.º 1
0
        private object Deserialize(ObjectProperties packItems)
        {
            object result = FormatterServices.GetUninitializedObject(_serializableTypeInfo.Type);

            //try to find a target for each packItem, if not found skip it
            foreach (var packItem in packItems)
            {
                FieldOrPropertyMemberInfo target;
                if (_packItemCandidates.TryGetValue(packItem.Key, out target))
                {
                    target.SetValueFor(result, ValueConverter.ConvertValueToTargetType(target.Type, packItem.Value));
                }
            }

            return(result);
        }
        private object MatchObject()
        {
            var elements = new Dictionary <string, object>();

            while (_reader.Read() && _reader.TokenType != JsonToken.EndObject)
            {
                if (_reader.TokenType == JsonToken.Comment)
                {
                    continue;
                }
                if (_reader.TokenType != JsonToken.PropertyName)
                {
                    throw Exceptions.UnexpectedTokenEncountered(_reader.TokenType);
                }

                var    key   = (string)_reader.Value;
                object value = MatchValue();
                elements.Add(key, value);
            }

            var objectProperties = new ObjectProperties(elements);

            //check if wrapped built-in type
            if (objectProperties.IsBuiltInTypeInPackformat)
            {
                return(objectProperties.GetBuiltInTypeUnpacked());
            }

            //build up object
            if (objectProperties.IsInPackformat)
            {
                var deserializerKey = new DeserializerKey(objectProperties.TypeName, objectProperties.Version);
                var typeUnpacker    = _deserializers.ResolveDeserializer(deserializerKey);

                //if we don't have a real builder we'll throw an exception when someone tries to get the data
                var builderFunc =
                    (typeUnpacker == null ? null : typeUnpacker.GetDeserializerFunc(_serializerInstanceStore))
                    ?? (i => { throw Exceptions.CannotFindDeserializer(i); });

                return(new ObjectInPackedForm(objectProperties, builderFunc));
            }
            //TODO handle the case when no name and/or version is present  - some JSON not serialized with shapeshifter
            throw Exceptions.InvalidInput();
        }
Ejemplo n.º 3
0
 public ObjectInPackedForm(ObjectProperties internalElements,
                           Func <ObjectProperties, object> deserializer)
 {
     _internalElements = internalElements;
     _deserializer     = deserializer;
 }
Ejemplo n.º 4
0
 public ShapeshifterReader(ObjectProperties elements)
 {
     _elements = elements;
 }