예제 #1
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.None)
            {
                return(null);
            }

            JObject          jo         = JObject.Load(reader);
            List <JProperty> properties = jo.Properties().ToList();

            //check to see if we've received a json blob like so:
            //  {
            //      "href": "http://server.com/api/resource/1234"
            //  }
            if ((properties.Count == 1) &&
                (properties[0].Name.ToLowerInvariant() == HrefElementName))
            {
                var jprop    = properties[0];
                var value    = jprop.Value.ToString();
                var instance = new Actionlink();
                instance.Href = value;
                return(instance);
            }
            else
            {
                var resourceType = objectType.GetGenericArguments()[0];
                var value        = jo.ToObject(resourceType);

                return(Activator.CreateInstance(objectType, value));
            }
        }
        public override object ReadJson( JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer )
        {
            if( reader.TokenType == JsonToken.None ) return null;

            JObject jo = JObject.Load( reader );
            List<JProperty> properties = jo.Properties().ToList();

            //check to see if we've received a json blob like so:
            //  {
            //      "href": "http://server.com/api/resource/1234"
            //  }
            if( ( properties.Count == 1 ) &&
                ( properties[ 0 ].Name.ToLowerInvariant() == HrefElementName ) )
            {
                var jprop = properties[ 0 ];
                var value = jprop.Value.ToString();
                var instance = new Actionlink();
                instance.Href = value;
                return instance;
            }
            else
            {
                var resourceType = objectType.GenericTypeArguments[ 0 ];
                var value = jo.ToObject( resourceType );

                return Activator.CreateInstance( objectType, value );
            }
        }