Mycom
Mycom

Reputation: 39

Json.Net serializes with strange charactes instead of brackets?

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: enter image description here 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

Answers (1)

paul
paul

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

Related Questions