c# - DataContractJsonSerializer human-readable json -
this question has answer here:
- how formatted json in .net using c#? 6 answers
basically dupe of this question 1 notable difference - have use datacontractjsonserializer
.
a simple
using (var stream = new memorystream()) { var serializer = new datacontractjsonserializer(typeof(person)); serializer.writeobject(stream, obj); ... return stream.toarray(); }
produced single line json, e.g. (when saved in file)
...{"blah":"v", "blah2":"v2"}...
what options make it
... { "blah":"v", "blah2":"v2" } ...
i can think of post-processing... there easier option? e.g. similar formatting xml produced datacontractserializer ?
using (var stream = new memorystream()) { var serializer = new datacontractjsonserializer(typeof(t)); // "beautify" using (var writer = new somekindofwriter(stream)) serializer.writeobject(writer, obj); ... return stream.toarray(); }
is there way make such somekindofwriter
beautify json when needed?
https://stackoverflow.com/a/38538454/6627992
you may use following standard method getting formatted json
jsonreaderwriterfactory.createjsonwriter(stream stream, encoding encoding, bool ownsstream, bool indent, string indentchars)
only set "indent==true"
try this
public readonly datacontractjsonserializersettings settings = new datacontractjsonserializersettings { usesimpledictionaryformat = true }; public void keep<tvalue>(tvalue item, string path) { try { using (var stream = file.open(path, filemode.create)) { var currentculture = thread.currentthread.currentculture; thread.currentthread.currentculture = cultureinfo.invariantculture; try { using (var writer = jsonreaderwriterfactory.createjsonwriter( stream, encoding.utf8, true, true, " ")) { var serializer = new datacontractjsonserializer(type, settings); serializer.writeobject(writer, item); writer.flush(); } } catch (exception exception) { debug.writeline(exception.tostring()); } { thread.currentthread.currentculture = currentculture; } } } catch (exception exception) { debug.writeline(exception.tostring()); } }
pay attention lines
var currentculture = thread.currentthread.currentculture; thread.currentthread.currentculture = cultureinfo.invariantculture; .... thread.currentthread.currentculture = currentculture;
you should use invariantculture avoid exception during deserialization on computers different regional settings. example, invalid format of double or datetime cause them.
for deserializing
public tvalue revive<tvalue>(string path, params object[] constructorargs) { try { using (var stream = file.openread(path)) { var currentculture = thread.currentthread.currentculture; thread.currentthread.currentculture = cultureinfo.invariantculture; try { var serializer = new datacontractjsonserializer(type, settings); var item = (tvalue) serializer.readobject(stream); if (equals(item, null)) throw new exception(); return item; } catch (exception exception) { debug.writeline(exception.tostring()); return (tvalue) activator.createinstance(type, constructorargs); } { thread.currentthread.currentculture = currentculture; } } } catch { return (tvalue) activator.createinstance(typeof (tvalue), constructorargs); } }
thanks!
Comments
Post a Comment