Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Too heavy caching in Json.NET breaks SerializerContractResolver #676

Closed
bnordli opened this issue Jan 28, 2016 · 1 comment
Closed

Too heavy caching in Json.NET breaks SerializerContractResolver #676

bnordli opened this issue Jan 28, 2016 · 1 comment
Labels

Comments

@bnordli
Copy link
Contributor

bnordli commented Jan 28, 2016

Currently, SerializerContractResolver is telling Json.NET to cache contracts across all instances of itself. This does not work, because using different options (mainly camel case behavior) gives different behavior. Therefore, whatever options is used the first time serializer is called will be used every time.

Example of a failing test.

Establish context = () =>
{
    to_serialize = new Dictionary<string, string> 
    {
        {"Key1", "Value1"},
        {"Key2", "Value2"},
    };
};
 
Because of = () =>
{
    serialized_version = serializer.ToJson(to_serialize);
    serialized_version_camel_case = serializer.ToJson(to_serialize, new SerializationOptions { UseCamelCase = true });
};
 
It should_serialize_the_dictionary = () =>
    serialized_version.ShouldEqual("{\"Key1\":\"Value1\",\"Key2\":\"Value2\"}");
 
It should_serialize_the_dictionary_in_camel_case = () =>
    serialized_version_camel_case.ShouldEqual("{\"key1\":\"Value1\",\"key2\":\"Value2\"}"); 

(It will not be serialized using camel case the second time.)

Quoting from http://www.newtonsoft.com/json/help/html/M_Newtonsoft_Json_Serialization_DefaultContractResolver__ctor_1.htm.

This setting can cause unexpected behavior if different instances of the resolver are suppose to produce different results

@bnordli
Copy link
Contributor Author

bnordli commented Dec 28, 2016

This is fixed in the ProCoSys fork.

bnordli added a commit to bnordli/Bifrost-statoil that referenced this issue Jan 4, 2017
…n of managing caching yourself. dolittle#676

(See http://www.newtonsoft.com/json/help/html/M_Newtonsoft_Json_Serialization_DefaultContractResolver__ctor_1.htm)

- Refactor SerializationOptions:
  - Move from specific properties (UseCamelCase, IncludeTypeNames) to flag enum (SerializationOptionFlags)
  - Make static instances for default configurations
  - Refactor out interface (ISerializationOptions) that is accepted by ISerializer and SerializerContractResolver

- Create a specific SagaSerializationOptions for use by sagas

- In Serializer, cache serializers based on different options, so that different behaviors are not getting mixed up by JSON
@einari einari added the Bug label Jan 4, 2017
@bnordli bnordli closed this as completed Jan 5, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants