Lightweight JSON parser
#1
Posted 28 June 2011 - 04:40 PM
I wrote a non-recursive, low-overhead JSON parser intended to run in conditions where memory is tight.
The parser supports reading from a string in memory or from a file on an SD card for example.
The source code and unit tests (see \Samples\JSONParserTest) are located here: http://netduinohelpe...list/changesets
Cheers,
-Fabien.
#2
Posted 28 June 2011 - 04:57 PM
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs
#3
Posted 28 June 2011 - 05:24 PM
#4
Posted 28 June 2011 - 06:25 PM
I have no benchmark / comparison with XML parsing in terms of performance / memory usage: in order to compare apples to apples, I'd have to write an XML parser using the same architecture really. I think I'll leave that as an exercise to the reader
My goal was to have a stand-alone, lightweight JSON parser, suitable for Netduino/Plus applications: since I could not find one on the InterWebs, I wrote one.
Cheers,
-Fabien.
#5
Posted 23 July 2011 - 03:18 AM
First of all thanks for this lib I found it very useful!
But I have a little problem. I get an System.ArgumentOutOfRangeException when I try to parse a string that has Unicode characters like "email": "user\u0040gmail.com".
Do you know what might cause this problem?
Thanks
#6
Posted 23 July 2011 - 04:55 PM
#7
Posted 23 July 2011 - 05:26 PM
#8
Posted 24 July 2011 - 08:04 PM
Yes it works perfect now!!! Thanks!Ok. I found the issue and checked-in the fix on CodePlex.
Can you please give it a try and confirm that everything is OK now?
Cheers,
-Fabien.
#9
Posted 24 July 2011 - 08:13 PM
#10
Posted 24 November 2011 - 11:41 AM
Awesome! Thanks for circling back
hi Fabien,
this is exactly what I need - though I am battling to figure out how to actually send the JSON request and capture the return.
I can manage normal HTTP requests - but of course going the following (.Net 4) route for fetching ia a no go:
public static void WebRequestinJson(string url, string postData)
{
StreamWriter requestWriter;
var webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;
if (webRequest != null)
{
webRequest.Method = "POST";
webRequest.ServicePoint.Expect100Continue = false;
webRequest.Timeout = 20000;
webRequest.ContentType = "application/json";
//POST the data.
using (requestWriter = new StreamWriter(webRequest.GetRequestStream()))
{
requestWriter.Write(postData);
}
}
}
Do you have any examples of sending a JSON request from a Netduino as a client?
Thanks
#11
Posted 25 November 2011 - 08:31 PM
hi Fabien,
this is exactly what I need - though I am battling to figure out how to actually send the JSON request and capture the return.
I can manage normal HTTP requests - but of course going the following (.Net 4) route for fetching ia a no go:
public static void WebRequestinJson(string url, string postData)
{
StreamWriter requestWriter;
var webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;
if (webRequest != null)
{
webRequest.Method = "POST";
webRequest.ServicePoint.Expect100Continue = false;
webRequest.Timeout = 20000;
webRequest.ContentType = "application/json";
//POST the data.
using (requestWriter = new StreamWriter(webRequest.GetRequestStream()))
{
requestWriter.Write(postData);
}
}
}
Do you have any examples of sending a JSON request from a Netduino as a client?
Thanks
Hi there,
I don't have a ready-made example to give you because I haven't had a need to do this yet ;-)
However, you should consider the following:
- The Netduino Plus has 28KB of RAM and using the System.Net classes for Web requests will generally result in out of memory exceptions. If you're trying to serialize data into JSON on top of that, your mileage will be further reduced... You should consider implementing a lightweight socket client to perform HTTP requests. See the KloutKlock sample application part of the netduino helpers library to see how to do that.
- Serializing to JSON: implement a simple serializer for the data types that you need to support and serialize to JSON format straight to a file on the SD card. Then, post from the serialized data on the SD card to the web service.
- Both of these methods will ensure that your application will remain stable and will not run out of memory.
#12
Posted 28 November 2011 - 09:58 AM
NeonMika.Webserver
> Control your N+ and write webservice methods easyily
> Receive data from you N+ (in XML or JSON)
> Browse the SD on your N+ directly in the browser and down - and upload files
If you need help with NeonMika.Webserver, please just leave a note in the thread and/or contact me via Skype
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- Mistakes teach you important lessons. Every time you make one, you are one step closer to your goal. ----
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
#13
Posted 29 November 2011 - 02:18 PM
Hi there,
I don't have a ready-made example to give you because I haven't had a need to do this yet ;-)
However, you should consider the following:
Does this make sense?
- The Netduino Plus has 28KB of RAM and using the System.Net classes for Web requests will generally result in out of memory exceptions. If you're trying to serialize data into JSON on top of that, your mileage will be further reduced... You should consider implementing a lightweight socket client to perform HTTP requests. See the KloutKlock sample application part of the netduino helpers library to see how to do that.
- Serializing to JSON: implement a simple serializer for the data types that you need to support and serialize to JSON format straight to a file on the SD card. Then, post from the serialized data on the SD card to the web service.
- Both of these methods will ensure that your application will remain stable and will not run out of memory.
Thanks Fabien,
I figured system.Net was not the way to go and have been playing with the NetMF WebRequest instead.
The page I need to collect is a simple 4k xhtml - so I could write a very basic XML parser (the tags etc will be consistent)
system.XML is of course too big for the NetduinoPlus too - so I was hoping you had an example where you were using your JSON parser with a simple web request that worked on the Netduiino (I assumed that you already had this code)
Thanks for the reply - it was infact helpful and has pointed me in a new direction (did not consider using the SD for storage)
The clases in KloutKlock definitely open some new options - so I'll see how i get on and post back - thanks for the help
#14
Posted 29 November 2011 - 03:30 PM
Thanks Fabien,
I figured system.Net was not the way to go and have been playing with the NetMF WebRequest instead.
The page I need to collect is a simple 4k xhtml - so I could write a very basic XML parser (the tags etc will be consistent)
system.XML is of course too big for the NetduinoPlus too - so I was hoping you had an example where you were using your JSON parser with a simple web request that worked on the Netduiino (I assumed that you already had this code)
Thanks for the reply - it was infact helpful and has pointed me in a new direction (did not consider using the SD for storage)
The clases in KloutKlock definitely open some new options - so I'll see how i get on and post back - thanks for the help
I've gone down the route your suggesting and even with a bare bones XML parser you'll be bouncing up against the memory constraints of the device. If you can get something even simpler to parse you'll be much better off.
Sometimes even though you love a device it may not be the right thing for the job and you may need to pick a device that has the capabilities you need. I originally wanted to use the Netduino Plus for a print agent that would pull XML from a service and dump it out to a receipt printer, but by the time I got web request and xml parsing coded I didn't have enough resources to do the IP based printing.
If you really want to use the device you may have to rethink how you'll get the data and process it in order to keep within the constraints of the device.
Regards,
Jim Lavin
http://diybrewery.com
#15
Posted 10 January 2012 - 12:31 PM
#16
Posted 14 May 2012 - 08:20 PM
#17
Posted 14 May 2012 - 09:18 PM
#18
Posted 21 August 2012 - 02:13 AM
#19
Posted 21 August 2012 - 03:13 AM
#20
Posted 16 January 2013 - 02:36 PM
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users