Visual basic data table to json object12/24/2023 ![]() ![]() Public override void WriteJson( JsonWriter writer, object dataset)ĭataTableConverter converter = new DataTableConverter() įoreach ( DataTable table in dataSet.Tables) Public class DataSetConverter : JsonConverter Return typeof( DataTable).IsAssignableFrom(valueType) Public override void WriteJson( JsonWriter writer, object dataTable)ĭataTable table = dataTable as DataTable ĭataRowConverter converter = new DataRowConverter() Public class DataTableConverter : JsonConverter Public override object ReadJson( JsonReader reader, Type objectType) / Reads the JSON representation of the object. Return typeof( DataRow).IsAssignableFrom(valueType) Public override bool CanConvert( Type valueType) / true if this instance can convert the specified value type otherwise, false. / Determines whether this instance can convert the specified value type. Writer.WritePropertyName(column.ColumnName) JsonSerializer ser = new JsonSerializer() įoreach ( DataColumn column in ) should be fixed in next ver of JSON.NET with writer.Serialize(object) *** HACK: need to use root serializer to write the column value Public override void WriteJson( JsonWriter writer, object dataRow) / Writes the JSON representation of the object. Public class DataRowConverter : JsonConverter Note the converters are only for serialization not deserialization: /// Here's are a few converters that can create DataSet/DataTable and DataRow JSON with JSON.NET and turns them into simple value arrays (ie ). So, my immediate need before I could use the JSON.NET parser for a couple of old apps was to create a parser that can serialize JSON from Datatables. The ASP.NET JavaScriptSerializer also supports extension via Converters. Luckily you can create your own converters, which is a common way of extensibility. For example, the ADO.NET objects, or a less obvious problem of how IDictionary types are handled (JSON.NET only supports string keys and then only those that result in valid JavaScript property/map names). Any JSON parser is likely to do well with most common types, but there may also be custom types or structures that are not supported or not well supported. You can create custom Converters that can be plugged into the parsing pipeline to serialize and optionally deserialize custom types. ![]() The really nice thing though - and the thing that really is missing in my parser - is extensibility. As you can see above the parser has a ton of control over how many parsing aspects are handled which is really cool. JSON.NET also has a simplified JavaScriptConvert class that has much simpler serialization and deserialization methods, but this wrapper doesn't have access to the configuration options, so creating a custom wrapper that does exactly what I need certainly helps. You can see some of the options above for things like null value handling and managing how deep recursive loops should be handled for example. One of the nice things about the JSON.NET parser is that there are a tons of configuration options that allow you to customize how JSON parsing occurs. Object result = json.Deserialize(reader, valueType) JsonTextReader reader = new JsonTextReader(sr) StringReader sr = new StringReader(jsonText) Json.ReferenceLoopHandling = Newtonsoft.Json. Json.NullValueHandling = Newtonsoft.Json. Public object Deserialize( string jsonText, Type valueType) JsonTextWriter writer = new JsonTextWriter(sw) Json.ReferenceLoopHandling = ReferenceLoopHandling.Ignore Json.MissingMemberHandling = Newtonsoft.Json. Json.ObjectCreationHandling = Newtonsoft.Json. Json.NullValueHandling = NullValueHandling.Ignore JsonSerializer json = new Newtonsoft.Json. So why not just replace the parser altogether? As it turns out I've been experimenting with different JSON serializer/deserializers and being able to keep a 'high level' wrapper object into which each of these parsers can be plugged into has been a great time saver as the interface that various application components use doesn't change one bit which is nice indeed.Īnyway, here's the basic code to provide JSON encoding and deserialization in a simple wrapper functions: public string Serialize( object value) There are also a few nice features like the ability to pretty-format the generated JSON for debugging purposes which is immensely helpful when sending data to the client during development.Īlthough I'm keeping my original parser class (including original basic functionality) JSON.NET can now be optionally plugged in. ![]() My own parser has served me well, but JSON.NET is a much cleaner design and more flexible especially when dealing with extensibility. I've been thinking about replacing the JSON serializer in my internal codebase for some time and finally put a little effort into allowing JSON.NET from James Newton King to be plugged into my Ajax framework as a replaceable engine. ![]()
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |