Ejemplo n.º 1
0
        /// <see cref="JsonConverter{T}.WriteJson(JsonWriter, T, JsonSerializer)"/>
        public override void WriteJson(JsonWriter writer, [AllowNull] VectisBase value, JsonSerializer serializer)
        {
            writer.WriteStartObject();
            var converterInfo = ConverterInfo.GetConverterInfo(value.GetType());

            // Poly writer
            writer.WritePropertyName(ConverterInfo.TypeInfoName);
            writer.WriteValue(converterInfo.TypeDiscriminator);

            foreach (var propertyInfo in converterInfo.NewtonsoftProperties.Values)
            {
                var propertyValue = propertyInfo.GetValue(value);

                if (propertyValue != null)
                {
                    var propertyName = propertyInfo.Name;

                    if (converterInfo.NewtonsoftPropertyNameToAttributeName.TryGetValue(propertyName, out string csPropertyName))
                    {
                        propertyName = csPropertyName;
                    }

                    writer.WritePropertyName(propertyName);
                    serializer.Serialize(writer, propertyValue);
                }
            }

            writer.WriteEndObject();
        }
        /// <see cref="JsonConverter{T}.Read(ref Utf8JsonReader, Type, JsonSerializerOptions)"/>
        public override VectisBase Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            if (reader.TokenType != JsonTokenType.StartObject)
            {
                throw new JsonException();
            }

            reader.Read();
            if (reader.TokenType != JsonTokenType.PropertyName)
            {
                throw new JsonException();
            }

            if (reader.GetString() != ConverterInfo.TypeInfoName)
            {
                throw new JsonException();
            }

            reader.Read();
            if (reader.TokenType != JsonTokenType.String)
            {
                throw new JsonException();
            }

            var converterInfo = ConverterInfo.GetConverterInfo(reader.GetString());
            var value         = Activator.CreateInstance(converterInfo.Type) as VectisBase;

            while (reader.Read())
            {
                if (reader.TokenType == JsonTokenType.PropertyName)
                {
                    var propertyName = reader.GetString();

                    reader.Read();

                    if (converterInfo.SystemTextJsonAttributeNameToPropertyName.TryGetValue(propertyName, out string csPropertyName))
                    {
                        propertyName = csPropertyName;
                    }

                    if (propertyName[0] == '_' && ConverterInfo.CosmosSystemDefinedElements.Contains(propertyName))
                    {
                        continue;
                    }

                    if (reader.TokenType != JsonTokenType.Null)
                    {
                        var propertyInfo = converterInfo.SystemTextJsonProperties[propertyName];
                        propertyInfo.SetValue(value, JsonSerializer.Deserialize(ref reader, propertyInfo.PropertyType, options));
                    }
                }
                else if (reader.TokenType == JsonTokenType.EndObject)
                {
                    return(value);
                }
            }

            throw new JsonException();
        }
Ejemplo n.º 3
0
        /// <see cref="JsonConverter{T}.ReadJson(JsonReader, Type, T, bool, JsonSerializer)"/>
        public override VectisBase ReadJson(JsonReader reader, Type objectType, [AllowNull] VectisBase existingValue, bool hasExistingValue, JsonSerializer serializer)
        {
            if (reader.TokenType != JsonToken.StartObject)
            {
                throw new JsonException($"NewtonsoftJsonConverter.ReadJson - first reader.TokenType should be '{JsonToken.StartObject}' and was '{reader.TokenType}' for type '{objectType}'");
            }

            reader.Read();
            if (reader.TokenType != JsonToken.PropertyName)
            {
                throw new JsonException($"NewtonsoftJsonConverter.ReadJson - first reader.TokenType should be '{JsonToken.PropertyName}' and was '{reader.TokenType}' for type '{objectType}'");
            }

            if (reader.Value.ToString() != ConverterInfo.TypeInfoName)
            {
                throw new JsonException($"NewtonsoftJsonConverter.ReadJson - first reader.Value.ToString() should be '{ConverterInfo.TypeInfoName}' and was '{reader.Value}' for type '{objectType}'");
            }

            var converterInfo = ConverterInfo.GetConverterInfo(reader.ReadAsString());
            var value         = Activator.CreateInstance(converterInfo.Type) as VectisBase;

            while (reader.Read())
            {
                if (reader.TokenType == JsonToken.PropertyName)
                {
                    var propertyName = reader.Value.ToString();

                    reader.Read();

                    if (propertyName[0] == '_' && ConverterInfo.CosmosSystemDefinedElements.Contains(propertyName))
                    {
                        continue;
                    }

                    if (converterInfo.NewtonsoftAttributeNameToPropertyName.TryGetValue(propertyName, out string csPropertyName))
                    {
                        propertyName = csPropertyName;
                    }

                    if (reader.TokenType != JsonToken.Null)
                    {
                        var propertyInfo = converterInfo.NewtonsoftProperties[propertyName];
                        propertyInfo.SetValue(value, serializer.Deserialize(reader, propertyInfo.PropertyType));
                    }
                }
                else if (reader.TokenType == JsonToken.EndObject)
                {
                    return(value);
                }
            }

            throw new JsonException($"NewtonsoftJsonConverter.ReadJson - ended without reader.TokenType equal to '{JsonToken.EndObject}' for type '{objectType}'");
        }
        /// <see cref="JsonConverter{T}.Write(Utf8JsonWriter, T, JsonSerializerOptions)"/>
        public override void Write(Utf8JsonWriter writer, VectisBase value, JsonSerializerOptions options)
        {
            writer.WriteStartObject();
            var converterInfo = ConverterInfo.GetConverterInfo(value.GetType());

            // Poly writer
            writer.WriteString(ConverterInfo.TypeInfoName, converterInfo.TypeDiscriminator);

            foreach (var propertyInfo in converterInfo.SystemTextJsonProperties.Values)
            {
                var propertyValue = propertyInfo.GetValue(value);

                if (propertyValue != null)
                {
                    var propertyName = propertyInfo.Name;

                    if (converterInfo.SystemTextJsonPropertyNameToAttributeName.TryGetValue(propertyName, out string csPropertyName))
                    {
                        propertyName = csPropertyName;
                    }

                    writer.WritePropertyName(propertyName);

                    try
                    {
                        JsonSerializer.Serialize(writer, propertyValue, /*propertyInfo.PropertyType,*/ options);
                    }
                    catch (Exception exception)
                    {
                        Console.WriteLine(exception.Message);
                    }
                }
            }

            writer.WriteEndObject();
        }