c# - serialize to stream or not -
i found 2 different examples of how serialize data. 1 using stream , other not?
stream1.position = 0; streamreader sr = new streamreader(stream1); console.write("json form of person object: "); console.writeline(sr.readtoend()); public static string tojson(this object obj, int recursiondepth = 100) { javascriptserializer serializer = new javascriptserializer(); serializer.recursionlimit = recursiondepth; return serializer.serialize(obj); } public static list<t> tolistobject<t>(this string obj, int recursiondepth = 100) { javascriptserializer serializer = new javascriptserializer(); serializer.recursionlimit = recursiondepth; list<t> returnlist = serializer.deserialize<list<t>>(obj); return returnlist; } why use stream added step?
using stream or reader/writer api may convenient if talking backed such file or network-socket, , data large (i.e. don't want have load huge string in memory).
the string-based api more convenient when handling small amounts of data, typical in ajax/http scenarios. note serializers do not have api directly exposes string input/output, such common scenario json has been made easy you.
one convenience wrapper of other. way around implementation detail, although hope primary internal code stream- or reader/writer- based (to allow large documents without overhead).
use whichever convenient purpose.
Comments
Post a Comment