Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

[DRAFT]JsonExtensionData should allow non-JsonElement types during serialization - #38262

Closed
MarcoRossignoli wants to merge 2 commits into
dotnet:masterfrom
MarcoRossignoli:json_extensions
Closed

MarcoRossignoli wants to merge 2 commits into
dotnet:masterfrom
MarcoRossignoli:json_extensions

Conversation

@MarcoRossignoli

@MarcoRossignoli MarcoRossignoli commented Jun 5, 2019 •

Copy link
Copy Markdown
Member

working on https://github.com/dotnet/corefx/issues/38238

THIS IS A TMP DRAFT PR(INCOMPLETE) TO DISCUSS THE ISSUE, I'LL CLOSE.

Fix serialization it's not a great problem, the hard part is understand how to handle "deserialization" of numbers/object.
I tested Newtonsoft.Json and I would know if should be ok behave in the same way.

 public class TestModel
    {
        public string Name { get; set; } = "Test";

        [JsonExtensionDataAttribute]
        public Dictionary<string, object> Extensions { get; set; } = new Dictionary<string, object>()
        {
            // ["foo"] = 1.1
        };
    }

    class Program
    {
        static void Main(string[] args)
        {
            string s = @"{""Name"":""Test"",""foo"":1e-005}";
            var s1 = JsonConvert.DeserializeObject<TestModel>(s);
            Console.WriteLine(s1.Extensions["foo"].GetType());

            s = @"{""Name"":""Test"",""foo"":1.1}";
            s1 = JsonConvert.DeserializeObject<TestModel>(s);
            Console.WriteLine(s1.Extensions["foo"].GetType());

            s = @"{""Name"":""Test"",""foo"":1}";
            s1 = JsonConvert.DeserializeObject<TestModel>(s);
            Console.WriteLine(s1.Extensions["foo"].GetType());

            s = @"{""Name"":""Test"",""foo"":-1}";
            s1 = JsonConvert.DeserializeObject<TestModel>(s);
            Console.WriteLine(s1.Extensions["foo"].GetType());

            s = @"{""Name"":""Test"",""foo"":""text""}";
            s1 = JsonConvert.DeserializeObject<TestModel>(s);
            Console.WriteLine(s1.Extensions["foo"].GetType());

            s = @"{""Name"":""Test"",""foo"":{""prop"":""text""}}";
            s1 = JsonConvert.DeserializeObject<TestModel>(s);
            Console.WriteLine(s1.Extensions["foo"].GetType());

            s = @"{""Name"":""Test"",""foo"":true}";
            s1 = JsonConvert.DeserializeObject<TestModel>(s);
            Console.WriteLine(s1.Extensions["foo"].GetType());

            s = @"{""Name"":""Test"",""foo"":false}";
            s1 = JsonConvert.DeserializeObject<TestModel>(s);
            Console.WriteLine(s1.Extensions["foo"].GetType());
        }
    }

Result

System.Double
System.Double
System.Int64
System.Int64
System.String
Newtonsoft.Json.Linq.JObject <- we have JsonElement
System.Boolean
System.Boolean

@steveharter @ahsonkhan before go on and prepare a complete PR I would like to know if I'm on right way or if there was another plan, thank's!

Note that in the one use case that MVC uses this feature, it stores arbitrary content (strings or numbers)

Or should we allow serialization and left deserialization to add only JsonElement on deserialization?

cc: @pranavkm

@pranavkm

pranavkm commented Jun 5, 2019

Copy link
Copy Markdown

@MarcoRossignoli I wouldn't change the semantics of how deserialization occurs. The only thing that's necessary to have a good experience with the attribute is to remove this check: https://github.com/dotnet/corefx/blob/master/src/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.HandleDictionary.cs#L141

@MarcoRossignoli

MarcoRossignoli commented Jun 5, 2019 •

Copy link
Copy Markdown
Member Author

@pranavkm I don't think that remove that check is enough...only entry value of type JsonElement has got WriteAsProperty, we should serialize in some way values, right?
If entry values are object I think we need to serialize extension prop like "normal dictionary".
BTW if deserialization is not a problem(we deserialize all values as JsonElement so users will need to cast to it and call correct Get[Type]) update is less intrusive and the code is something similar

DictionaryEntry entry = ((IDictionaryEnumerator)state.Current.Enumerator).Entry;
if (entry.Value is JsonElement element)
{
Debug.Assert(entry.Key is string);
string propertyName = (string)entry.Key;
element.WriteAsProperty(propertyName, writer);
}
else if (typeof(IDictionary<string, object>).IsAssignableFrom(state.Current.JsonPropertyInfo.DeclaredPropertyType))
{
WriteDictionary(jsonPropertyInfo, elementClassInfo, options, writer, ref state);
}
else
{
ThrowHelper.ThrowInvalidOperationException_SerializationDataExtensionPropertyInvalid(state.Current.JsonClassInfo, entry.Value.GetType());
}
}
else
{
WriteDictionary(jsonPropertyInfo, elementClassInfo, options, writer, ref state);
}

If you confirm that it's ok keep deserialization asis(all JsonElement) I can go on.

@pranavkm

pranavkm commented Jun 5, 2019

Copy link
Copy Markdown

If you confirm that it's ok keep deserialization asis(all JsonElement) I can go on.

That seems perfectly reasonable.

@ahsonkhan

Copy link
Copy Markdown

Fix serialization it's not a great problem, the hard part is understand how to handle "deserialization" of numbers/object.

As @pranavkm mentioned, this should only be fixed for the serialization side. The deserialization should continue to be as JsonElement (and not other strongly typed types).

Assert.IsType<double>(obj.MyOverflow["key"]);
Assert.Equal(1e-005, (double)obj.MyOverflow["key"]);

obj = JsonSerializer.Parse<ClassWithExtensionPropertyAsObject>(@"{""MyOverflow"":{""key"":1.1}}");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not want to "guess" here. We just want to create JsonElement on deserialize.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok understood thanks

@ahsonkhan ahsonkhan added this to the 3.0 milestone Jun 5, 2019
@MarcoRossignoli

MarcoRossignoli commented Jun 5, 2019 •

Copy link
Copy Markdown
Member Author

Thank's to all for fast feedback, close this for now because I get all answers I needed, tomorrow(CET) I'll PR final code.

@@ -40,34 +40,27 @@ private static bool HandleDictionary(
// Handle DataExtension.
if (ReferenceEquals(jsonPropertyInfo, state.Current.JsonClassInfo.DataExtensionProperty))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we just need to remove lines 41-45 (and the WriteExtensionData method) + update tests.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened another PR #38306

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants