Reputation: 39
i have a strange problem with the json.net serializer. Here is the code that serializers. I think there is nothing wrong with it:
var info = new Info("Peter", 25);
var filePath = Path.Combine(Application.dataPath, "test.xml");
FileStream stream = new FileStream(Path.Combine(Application.dataPath, "test.xml"), FileMode.Open);
var writer = new BsonWriter(stream);
var serializer = new JsonSerializer();
serializer.Serialize(writer, info);
stream.Close();
and the Info class:
public class Info
{
public string name;
public int age;
public Info(string name, int age)
{
this.name = name;
this.age = age;
}
}
when this serializes some strange charactes come out instead of the json valid brackets. It's kind of unreadable for a lot of data: also the age doesn't seem to be serialized. Is this a problem with the used characterset or something? For me its very handy if i can check wheather everything serializes correctly by looking into the files. Also setting the indent setting for the serializer doesn't make a different. How can i fix this?
Upvotes: 0
Views: 60
Reputation: 22001
I think your problem is that you are using BsonWriter
and expecting a readable text file, try using a TextWriter
instead:
var info = new Info("Peter", 25);
var filePath = Path.Combine(Application.dataPath, "test.xml");
TextWriter writer = File.CreateText(Path.Combine(Application.dataPath, "test.xml"));
var serializer = new JsonSerializer();
serializer.Serialize(writer, info);
writer.Close();
Upvotes: 1