2

I want to use the DataContractJsonSerializer to serialize to file in JsonFormat. The problem is that the WriteObjectmethod only has 3 options XmlWriter, XmlDictionaryWriter and Stream.

To get what I want I used the following code:

var js = new DataContractJsonSerializer(typeof(T), _knownTypes);
using (var ms = new MemoryStream())
{
   js.WriteObject(ms, item);
   ms.Position = 0;
   using (var sr = new StreamReader(ms))
   {
      using (var writer = new StreamWriter(path, false))
      {
         string jsonData = sr.ReadToEnd();
         writer.Write(jsonData);             
       }
   }
}

Is this the only way or have I missed something?

dbc
  • 104,963
  • 20
  • 228
  • 340
Amit Raz
  • 5,370
  • 8
  • 36
  • 63

2 Answers2

7

Assuming you're just trying to write the text to a file, it's not clear why you're writing it to a MemoryStream first. You can just use:

var js = new DataContractJsonSerializer(typeof(T), _knownTypes);
using (var stream = File.Create(path))
{
    js.WriteObject(stream, item);
}

That's rather simpler, and should do what you want...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    Thanks! It was late last night :)... One more question. Is it possible to have the TextFile indented? cause right now I am getting a very long line... – Amit Raz Jun 30 '15 at 06:53
  • @AmitRaz: Not that I know of, I'm afraid - you might want to consider using Json.NET instead :) – Jon Skeet Jun 30 '15 at 07:12
  • I tried using Json.NET unfortunately since we used DataContractSerialization up to now some people got away with setting the keys of dictionaries to Tuples and such, things which Json.Net does not handle very nicely... – Amit Raz Jun 30 '15 at 07:14
  • @AmitRaz: Hmm. I would definitely try to make progress towards moving towards Json.NET in the longer term. But I appreciate it may not be something you can do right now. – Jon Skeet Jun 30 '15 at 08:13
  • I have bee trying to make it work for several days. The problem is that out application is already out and installed in customer locations so backwards compatibility and No Data loss is a must... – Amit Raz Jun 30 '15 at 08:41
  • @JonSkeet: while I don't disagree with your recommendation of Json.NET, I believe it is possible to produce a formatted (i.e., indented) JSON file without it. (See attached answer.) – kmote Mar 13 '17 at 19:50
  • @kmote: Fair enough - I'd definitely still use Json.NET in preference :) – Jon Skeet Mar 13 '17 at 20:04
5

I am actually quite terrified to claim to know something that Jon Skeet doesn't, but I have used code similar to the following which produces the Json text file and maintains proper indentation:

var js = new DataContractJsonSerializer(typeof(T), _knownTypes);
using (var stream = File.Create(path))
{
    using (var writer = JsonReaderWriterFactory.CreateJsonWriter(stream, Encoding.UTF8, true, true, "\t"))
    {
        js.WriteObject(writer, item);
        writer.Flush();
    }
}

(as suggested here.)

Community
  • 1
  • 1
kmote
  • 16,095
  • 11
  • 68
  • 91