1

I have a struct, Vector2 that when serialized to JSON produces a different outpout on different platforms.

Struct I am serializing:

#if XNADESIGNPROVIDED
[System.ComponentModel.TypeConverter(typeof(Microsoft.Xna.Framework.Design.Vector2TypeConverter))]
#endif
[DataContract]
[DebuggerDisplay("{DebugDisplayString,nq}")]
[DebuggerStepThrough]
    public struct Vector2 : IEquatable<Vector2>
    {
        [DataMember]
        public float X;

        [DataMember]
        public float Y;
}

Serialization

This struct is part of a class, that is part of a collection. This code is included on both platforms, and run on both platforms.

private static readonly JsonSerializerSettings JsonSerializerSettings = new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore };

public string ToJson()
    { 
        return JsonConvert.SerializeObject(this, JsonSerializerSettings);
    }

public static MenuEntryCollection FromJson(string in_Data)
    {
        return JsonConvert.DeserializeObject<MenuEntryCollection>(in_Data);
    }

Result from UWP Serializer:

   "SizeRelative": {
        "X": 224.0,
        "Y": 63.9999847
    },

Result from Desktop Seralizer:

"SizeRelative": "384, 64",

Using Newtonsoft Json.net, 10.0.3

Why is the output different? And why can one, not load the other?

Jarryd
  • 572
  • 4
  • 13
  • 1
    Could you provide the serialization code also? – mehran Nov 29 '17 at 03:23
  • 2
    *Why is the output different?* Probably because your `Vector2` has an associated `TypeConverter` not shown in your question. See [Newtonsoft.JSON cannot convert model with TypeConverter attribute](https://stackoverflow.com/q/31325866/3744182) for why this matters. – dbc Nov 29 '17 at 03:33
  • 1
    Further, I believe, even within Json.NET, support for serialization using an associated `TypeConverter` was originally only available in the .Net full framework build(s). For .Net core (and UWP I reckon) it will be added in the **next** release, as explained in [.NET Standard 2.0 #1423](https://github.com/JamesNK/Newtonsoft.Json/issues/1423). So if you are using Json.NET in both UWP and desktop, that might explain the inconsistency. – dbc Nov 29 '17 at 03:49
  • @dbc I've added additional information. You were also correct about the typeconverter attribute, and after removing it I can now load the JSON generated by the UWP build. I did not understand that it was relevant until now. – Jarryd Nov 29 '17 at 07:12

1 Answers1

1

JSON.NET uses an associated TypeConverter if that is available on the class/struct, this was originally only available in the .Net full framework.

For .Net core and UWP it will be added in the next release, as a part of .NET Standard 2.0, so if you are using Json.NET in both UWP and desktop, that is probably the issue.

Check for a TypeConverter on your class/struct. Removing it should make JSON.NET fall-back to the default serializer which is probably what you are getting on your UWP builds.

Daniel Armstrong
  • 829
  • 9
  • 22