Exemple #1
0
            public bool MoveNext()
            {
                var index = _index;

                if (_index++ < _array.Count)
                {
                    _current = _array[index] as DefaultJsonToken;
                    return(true);
                }

                _current = null;
                return(false);
            }
Exemple #2
0
        protected override JsonObjectContract CreateObjectContract(Type objectType)
        {
            var contract = base.CreateObjectContract(objectType);

            if (typeof(JsonModel).IsAssignableFrom(objectType))
            {
                contract.ExtensionDataGetter = instance =>
                {
                    return(instance is JsonModel model && model.ExtensionData != null
                        ? model.ExtensionData.Select(x => KeyValuePair.Create(x.Key as object, (x.Value as DefaultJsonToken).Token as object))
                        : null);
                };

                var skippedProperties = objectType.GetCustomAttribute <JsonSkippedPropertiesAttribute>()?.Properties;
                contract.ExtensionDataSetter = (instance, key, value) =>
                {
                    if (instance is not JsonModel model)
                    {
                        return;
                    }

                    if (skippedProperties != null && Array.IndexOf(skippedProperties, key) != -1)
                    {
                        return;
                    }

                    IJsonToken token;
                    if (value == null)
                    {
                        token = null;
                    }
                    else if (value is JToken jToken)
                    {
                        token = DefaultJsonToken.Create(jToken, _serializer.UnderlyingSerializer);
                    }
                    else
                    {
                        token = DefaultJsonToken.Create(JToken.FromObject(value, _serializer.UnderlyingSerializer), _serializer.UnderlyingSerializer);
                    }

                    model.ExtensionData.Add(key, token);
                };
            }

            return(contract);
        }
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var token = JToken.ReadFrom(reader);

            return(DefaultJsonToken.Create(token, serializer));
        }
Exemple #4
0
 public void Reset()
 {
     _index   = 0;
     _current = null;
 }