1

I am trying to serialize an object with a static System.Version field:

[JsonObject(MemberSerialization.OptIn)]
public class MyObj
{
    [JsonProperty]
    private static string testStr;
    [JsonProperty(ItemConverterType = typeof(VersionConverter))]
    private static Version ver = System.Reflection.Assembly...Version;

    // some other non-serialized fields
    // ...
}

I have learnt from this question that Version needs a custom converter, which I added as ItemConverterType. However, when I try to serialize it like this, it fails with the error: Expected Version object value:

var o = MyObj();
using (StreamWriter file = File.CreateText(filename))
{
    JsonSerializer serializer = new JsonSerializer { Formatting = Formatting.Indented };
    serializer.Serialize(file, o); // error
}    

It works fine if I modify the attributes of the field like this:

public class MyObj
{
    ...
    [JsonProperty]
    [JsonConverter(typeof(VersionConverter))]
    private static Version ver = System.Reflection.Assembly...Version;
    ...

I am new to attributes. Can you please shed some light as to why the first one fails ? I am quite sure I am not using Json.NET correctly, but can't figure out why.

Community
  • 1
  • 1
Ciprian Tomoiagă
  • 3,773
  • 4
  • 41
  • 65
  • Seems as though it only serializes static **properties** when `[JsonProperty]` is applied. See [Why can't JSON .Net serialize static or const member variables?](https://stackoverflow.com/questions/24336597) Change your fields to properties and you should be set to go. – dbc Jul 12 '16 at 03:49
  • @dbc thank you! note that serializing the `testStr` works fine, and the 2nd version, with the converter specified also works ok. It even works without the converter (only `JsonProperty`) for serializing, but not for deserializing. So there's something about `ItemConverterType` – Ciprian Tomoiagă Jul 12 '16 at 03:53
  • 1) Really? Does that mean the answer I linked to is obsolete? 2) [`ItemConverterType`](http://www.newtonsoft.com/json/help/html/P_Newtonsoft_Json_JsonPropertyAttribute_ItemConverterType.htm) allows you to specify a converter to use for *collection items*. See [Proper way of using Newtonsoft Json ItemConverterType](https://stackoverflow.com/questions/24639750). Since `string` and `Version` aren't treated as collections, it's ignored. For a converter on the property itself use `[JsonConverter]`. – dbc Jul 12 '16 at 03:58
  • 1
    1) yes, the **static** `string` and `Version` are successfully serialized and populated on deserializing with `Json.NET` **v8.0.3**. 2) ah, I didn't realise it was for collections only. Post *2)* as an answer – Ciprian Tomoiagă Jul 12 '16 at 04:06
  • Well you're right: https://dotnetfiddle.net/0UPaz9. It was fixed in [Json.NET 6.0.4](https://github.com/JamesNK/Newtonsoft.Json/releases/tag/6.0.4). Maybe I'll add an answer to that old question. – dbc Jul 12 '16 at 04:08

1 Answers1

1

ItemConverterType allows you to specify a converter to use for collection items. See Proper way of using Newtonsoft Json ItemConverterType. Since string and Version aren't treated as collections, it's ignored. For a converter on the property itself use [JsonConverter].

Conversely, if you had a static List<Version> versions it would be appropriate to use [JsonProperty(ItemConverterType = typeof(VersionConverter))].

dbc
  • 104,963
  • 20
  • 228
  • 340