I have an object:
class Node
{
public Node(string text)
{
Text = new List<string> { text };
}
public List<string> Text { get; set; }
}
When I attempt to round-trip an instance of this object to JSON using Json.NET like so:
var root = new Node("");
var json = JsonConvert.SerializeObject(root);
root = JsonConvert.DeserializeObject<Node>(json);
I get the following error:
Unhandled Exception: Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: [. Path 'Text', line 2, position 11.
at Newtonsoft.Json.JsonTextReader.ReadStringValue(ReadType readType)
at Newtonsoft.Json.JsonTextReader.ReadAsString()
at Newtonsoft.Json.JsonReader.ReadForType(JsonContract contract, Boolean hasConverter)
For some reason it just can't handle that List<string> Text
field and I can't for the life of me figure it out.
I'm literally trying to deserialise the string it JUST serialised. I could try write a custom converter but it doesn't seem like it should be necessary here.
Is there perhaps an attribute I can use to help it along?
Edit:
Created a new (.NET Framework Console App) project with just one Program.cs
file with the following code in it:
using Newtonsoft.Json; // Version: 12.0.3
using System.Collections.Generic;
namespace ConsoleApp1
{
class Node
{
public Node(string text)
{
Text = new List<string> { text };
}
public List<string> Text { get; set; }
}
class Program
{
static void Main()
{
var root = new Node("");
var json = JsonConvert.SerializeObject(root);
root = JsonConvert.DeserializeObject<Node>(json);
}
}
}
Am still getting the same error.