Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,10 @@ public void Serialize_ShouldNotCamelCaseBindVars_WhenSerializingPostCursorBody()
};
var serialization = new JsonNetApiClientSerialization();

byte[] jsonBytes = serialization.Serialize(body, new ApiClientSerializationOptions(true, true));
byte[] jsonBytes = serialization.Serialize(body, new ApiClientSerializationOptions(
useCamelCasePropertyNames: true,
ignoreNullValues: true,
camelCasePropertyNamesOfObjectValuesInDictionaries: false));

string jsonString = Encoding.UTF8.GetString(jsonBytes);

Expand All @@ -169,6 +172,31 @@ public void Serialize_ShouldNotCamelCaseBindVars_WhenSerializingPostCursorBody()
Assert.DoesNotContain("dontCamelCaseKey", jsonString);
}

[Fact]
public void Serialize_ShouldCamelCaseBindVars_WhenSerializingPostCursorBody()
{
var body = new PostCursorBody
{
BindVars = new Dictionary<string, object>
{
["DontCamelCaseKey"] = new { DontCamelCaseMe = true }
}
};
var serialization = new JsonNetApiClientSerialization();

byte[] jsonBytes = serialization.Serialize(body, new ApiClientSerializationOptions(
useCamelCasePropertyNames: true,
ignoreNullValues: true,
camelCasePropertyNamesOfObjectValuesInDictionaries: true));

string jsonString = Encoding.UTF8.GetString(jsonBytes);

Assert.Contains("DontCamelCaseKey", jsonString);
Assert.DoesNotContain("dontCamelCaseKey", jsonString);
Assert.Contains("dontCamelCaseMe", jsonString);
Assert.DoesNotContain("DontCamelCaseMe", jsonString);
}

[Fact]
public void Serialize_ShouldNotCamelCaseParams_WhenSerializingPostTransactionBody()
{
Expand All @@ -182,7 +210,10 @@ public void Serialize_ShouldNotCamelCaseParams_WhenSerializingPostTransactionBod

var serialization = new JsonNetApiClientSerialization();

byte[] jsonBytes = serialization.Serialize(body, new ApiClientSerializationOptions(true, true));
byte[] jsonBytes = serialization.Serialize(body, new ApiClientSerializationOptions(
useCamelCasePropertyNames: true,
ignoreNullValues: true,
camelCasePropertyNamesOfObjectValuesInDictionaries: false));

string jsonString = Encoding.UTF8.GetString(jsonBytes);

Expand All @@ -192,6 +223,33 @@ public void Serialize_ShouldNotCamelCaseParams_WhenSerializingPostTransactionBod
Assert.DoesNotContain("dontCamelCaseKey", jsonString);
}


[Fact]
public void Serialize_ShouldCamelCaseParams_WhenSerializingPostTransactionBody()
{
var body = new PostTransactionBody
{
Params = new Dictionary<string, object>
{
["DontCamelCaseKey"] = new { DontCamelCaseMe = true }
}
};

var serialization = new JsonNetApiClientSerialization();

byte[] jsonBytes = serialization.Serialize(body, new ApiClientSerializationOptions(
useCamelCasePropertyNames: true,
ignoreNullValues: true,
camelCasePropertyNamesOfObjectValuesInDictionaries: true));

string jsonString = Encoding.UTF8.GetString(jsonBytes);

Assert.Contains("DontCamelCaseKey", jsonString);
Assert.DoesNotContain("dontCamelCaseKey", jsonString);
Assert.Contains("dontCamelCaseMe", jsonString);
Assert.DoesNotContain("DontCamelCaseMe", jsonString);
}

[Fact]
public void Serialize_ShouldNotIgnoreNull_WhenSerializingPostCursorBody()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public abstract class ApiClientSerialization : IApiClientSerialization
useCamelCasePropertyNames: false,
ignoreNullValues: true,
useStringEnumConversion: false,
ignoreMissingMember: true);
ignoreMissingMember: true,
camelCasePropertyNamesOfObjectValuesInDictionaries: false);

/// <summary>
/// Deserializes the data structure contained by the specified stream
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,34 @@ public class ApiClientSerializationOptions
/// </summary>
public bool UseStringEnumConversion { get; set; }

/// <summary>
/// True to camel case the names of properties of object values
/// in dictionaries (i.e. CamelCaseMe => "camelCaseMe").
/// False to leave the names of properties of object values
/// in dictionaries as they are (i.e. DontCamelCaseMe => "DontCamelCaseMe")
/// </summary>
public bool CamelCasePropertyNamesOfObjectValuesInDictionaries { get; set; }

/// <summary>
/// Create serialization options.
/// </summary>
/// <param name="useCamelCasePropertyNames">Whether property names should be serialized using camelCase.</param>
/// <param name="ignoreNullValues">Whether null values should be ignored - i.e. not defined at all in the serialized string.</param>
/// <param name="useStringEnumConversion">Whether to serialize enum values to a string value instead of an integer.</param>
/// <param name="ignoreMissingMember">Whether missing members should be ignored.</param>
/// <param name="camelCasePropertyNamesOfObjectValuesInDictionaries">Whether to camel case the names of properties of object values in dictionaries.</param>
public ApiClientSerializationOptions(
bool useCamelCasePropertyNames,
bool ignoreNullValues,
bool useStringEnumConversion = false,
bool ignoreMissingMember = true)
bool ignoreMissingMember = true,
bool camelCasePropertyNamesOfObjectValuesInDictionaries = false)
{
UseCamelCasePropertyNames = useCamelCasePropertyNames;
IgnoreNullValues = ignoreNullValues;
UseStringEnumConversion = useStringEnumConversion;
IgnoreMissingMember = ignoreMissingMember;
CamelCasePropertyNamesOfObjectValuesInDictionaries = camelCasePropertyNamesOfObjectValuesInDictionaries;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ namespace ArangoDBNetStandard.Serialization
/// </summary>
public class CamelCasePropertyNamesExceptDictionaryContractResolver : DefaultContractResolver
{
public CamelCasePropertyNamesExceptDictionaryContractResolver()
private ApiClientSerializationOptions _serializationOptions;
public CamelCasePropertyNamesExceptDictionaryContractResolver(ApiClientSerializationOptions serializationOptions)
{
NamingStrategy = new CamelCaseNamingStrategy();
_serializationOptions = serializationOptions;
}

protected override JsonDictionaryContract CreateDictionaryContract(Type objectType)
{
JsonDictionaryContract contract = base.CreateDictionaryContract(objectType);
contract.DictionaryKeyResolver = propertyName => propertyName;
contract.ItemConverter = new DictionaryValueConverter();
contract.ItemConverter = new DictionaryValueConverter(_serializationOptions);
return contract;
}
}
Expand Down
41 changes: 34 additions & 7 deletions arangodb-net-standard/Serialization/DictionaryValueConverter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Newtonsoft.Json;
using System;
using Newtonsoft.Json.Converters;

namespace ArangoDBNetStandard.Serialization
{
Expand All @@ -9,10 +10,12 @@ namespace ArangoDBNetStandard.Serialization
/// </summary>
public class DictionaryValueConverter : JsonConverter
{
private static JsonSerializer _serializer = new JsonSerializer
private ApiClientSerializationOptions _serializationOptions;

public DictionaryValueConverter(ApiClientSerializationOptions serializationOptions)
{
NullValueHandling = NullValueHandling.Include
};
_serializationOptions = serializationOptions;
}

public override bool CanConvert(Type objectType)
{
Expand All @@ -26,9 +29,33 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
// Use our local serializer for writing instead of the passed-in serializer
_serializer.Serialize(writer, value);
// Use a local serializer for writing instead of the passed-in serializer
JsonSerializer mySerializer;
if (_serializationOptions != null && _serializationOptions.CamelCasePropertyNamesOfObjectValuesInDictionaries)
{
mySerializer = new JsonSerializer
{
MissingMemberHandling = _serializationOptions.IgnoreMissingMember ? MissingMemberHandling.Ignore : MissingMemberHandling.Error,
NullValueHandling = _serializationOptions.IgnoreNullValues ? NullValueHandling.Ignore : NullValueHandling.Include
};
if (_serializationOptions.UseStringEnumConversion)
{
var stringEnumConverter = new StringEnumConverter();
mySerializer.Converters.Add(stringEnumConverter);
}
if (_serializationOptions.UseCamelCasePropertyNames)
{
mySerializer.ContractResolver = new CamelCasePropertyNamesExceptDictionaryContractResolver(_serializationOptions);
}
}
else
{
mySerializer = new JsonSerializer
{
NullValueHandling = NullValueHandling.Include
};
}
mySerializer.Serialize(writer, value);
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public override string SerializeToString<T>(T item, ApiClientSerializationOption

if (serializationOptions.UseCamelCasePropertyNames)
{
jsonSettings.ContractResolver = new CamelCasePropertyNamesExceptDictionaryContractResolver();
jsonSettings.ContractResolver = new CamelCasePropertyNamesExceptDictionaryContractResolver(serializationOptions);
}

string json = JsonConvert.SerializeObject(item, jsonSettings);
Expand Down