Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > streaming json parser for j2me?

Reply
Thread Tools

streaming json parser for j2me?

 
 
Antti Järvinen
Guest
Posts: n/a
 
      03-02-2011
Is there json lib for microedition java that would let me de-serialize
the json format from a stream? This far I've only seen the org.json.me
lib that wants serialized content in as a String ; I'm kind of expecting
heap-size problems that InputStream might partly solve ; any other suggestions
besides keeping data chuncks small??

--
Costello the Warrior St:18/09 Dx:14 Co:18 In:8 Wi:12 Ch:7 Neutral
Dlvl:16 $:0 HP:129(129) Pw:52(52) AC:-6 Xp:14/83896 T:19462 Satiated
 
Reply With Quote
 
 
 
 
Joachim Lippold
Guest
Posts: n/a
 
      03-03-2011
Am 02.03.2011 15:10, schrieb Antti Jrvinen:
> Is there json lib for microedition java that would let me de-serialize
> the json format from a stream? This far I've only seen the org.json.me
> lib that wants serialized content in as a String ; I'm kind of expecting
> heap-size problems that InputStream might partly solve ; any other suggestions
> besides keeping data chuncks small??
>


Hi,

i recently wrote a small json parser for streams. It s not perfect and
not fully done (numbers are not parsed), but does all i need. Perhaps
you can build on this one:

http://bazaar.launchpad.net/~st-cdt/...rc/l/joe/json/

Regards Joe
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      03-03-2011
On 03/03/2011 05:40 AM, Joachim Lippold wrote:
> Am 02.03.2011 15:10, schrieb Antti Järvinen:
>> Is there json lib for microedition java that would let me de-serialize
>> the json format from a stream? This far I've only seen the org.json.me
>> lib that wants serialized content in as a String ; I'm kind of expecting
>> heap-size problems that InputStream might partly solve ; any other suggestions
>> besides keeping data chuncks small??


> i [sic] recently wrote a small json [sic] parser for streams. It s not perfect and not
> fully done (numbers are not parsed), but does all i [sic] need. Perhaps you can
> build on this one:
>
> http://bazaar.launchpad.net/~st-cdt/...rc/l/joe/json/


Are the set of whitespace characters in JSON different from those recognized
by the 'Character' test? (Just curious. I'm not adept at JSON.)

It's not really a good idea to ignore IOException on 'close()', nor to catch
just 'Exception' in lower-level code.

You might want to use standard library code for methods like 'hexValue()'
rather than rolling your own. I think API calls would help 'readNumber()',
too, but I'm not certain.

It's good, clean code except I am a bit puzzled by the presence of private
instance methods but the absence of public instance methods. It's not bad, I
suppose, but I'm not used to that.

--
Lew
Honi soit qui mal y pense.
 
Reply With Quote
 
Joachim Lippold
Guest
Posts: n/a
 
      03-03-2011
Am 03.03.2011 14:28, Lew wrote:
> On 03/03/2011 05:40 AM, Joachim Lippold wrote:
>> i [sic] recently wrote a small json [sic] parser for streams. It s not
>> perfect and not
>> fully done (numbers are not parsed), but does all i [sic] need.
>> Perhaps you can
>> build on this one:
>>
>> http://bazaar.launchpad.net/~st-cdt/...rc/l/joe/json/
>>

>
> Are the set of whitespace characters in JSON different from those
> recognized by the 'Character' test? (Just curious. I'm not adept at JSON.)


According to http://www.ietf.org/rfc/rfc4627.txt?number=4627
whitespace is defined as:
ws = *(
%x20 / ; Space
%x09 / ; Horizontal tab
%x0A / ; Line feed or New line
%x0D ; Carriage return
)

So i would have to remove "\b" and "\f" to be conform.

I wasn't aware anymore of this simple way to check for whitespace.
I will change it to Charackter.isWhitespace(). This makes things easier.

> It's not really a good idea to ignore IOException on 'close()', nor to
> catch just 'Exception' in lower-level code.


This happens in the finally{} and only if already something went wrong.
So a JSONParserException will be thrown anyway, encapsulating the real
cause.
This line only ensures that the inputstream is always closed when the
parse() method is left. That s why there is no further handling code for
the Exception.

And you are right, catching IOException would be better. I ll change it.

> You might want to use standard library code for methods like
> 'hexValue()' rather than rolling your own. I think API calls would help
> 'readNumber()', too, but I'm not certain.


I considered using Integer.parseInt(String, radix) for hexValue() but
parseInt() wants a String, and i have an int. I dont want to create a
String, just for invoking parseInt().

The readNumber() part is not yet done. I think NumberFormat.parse()
would be suitable.

> It's good, clean code except I am a bit puzzled by the presence of
> private instance methods but the absence of public instance methods.
> It's not bad, I suppose, but I'm not used to that.
>


Most of the private methods have only one purpose: Improve readability.
The name says what is done and encapsulates the implementation. Thus the
parse() method is much easier to read.

Furthermore the handling should be similar to a SAXParser, except the
ParserFactory stuff.

A little bit strange perhaps, but i like it .

Many Thanks for your review. I appreciate it.

Regards Joe
 
Reply With Quote
 
Paul Cager
Guest
Posts: n/a
 
      03-03-2011
On Mar 3, 1:28*pm, Lew <(E-Mail Removed)> wrote:
> On 03/03/2011 05:40 AM, Joachim Lippold wrote:
>
> > Am 02.03.2011 15:10, schrieb Antti Jrvinen:
> >> Is there json lib for microedition java that would let me de-serialize
> >> the json format from a stream? This far I've only seen the org.json.me
> >> lib that wants serialized content in as a String ; I'm kind of expecting
> >> heap-size problems that InputStream might partly solve ; any other suggestions
> >> besides keeping data chuncks small??

> > i [sic] recently wrote a small json [sic] parser for streams. It s not perfect and not
> > fully done (numbers are not parsed), but does all i [sic] need. Perhapsyou can
> > build on this one:

>
> >http://bazaar.launchpad.net/~st-cdt/...tastic-icecast...

>
> Are [sic] the set of whitespace characters in JSON different from those recognized
> by the 'Character' test? *(Just curious. *I'm not adept at JSON.)


http://www.lmgtfy.com/?q=json+whitespace

Sorry, sorry, sorry. I just couldn't resist _that_ sort of
opportunity.
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      03-03-2011
Paul Cager wrote:
> Lew wrote:
>> Are [sic] the set of whitespace characters in JSON different from those recognized
>> by the 'Character' test? *(Just curious. *I'm not adept at JSON.)

>


> http://www.lmgtfy.com/?q=json+whitespace
>
> Sorry, sorry, sorry. I just couldn't resist _that_ sort of
> opportunity
>


That's all right, it was very amusing if off point. I mentioned what
I did the way I did to remind the OP that 'Character.isWhitespace()'
might help. IOW, if the whitespace set differs, custom code is
needed, but if not, API calls work.

--
Lew
Good one, Paul!
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      03-03-2011
Joachim Lippold <(E-Mail Removed)> wrote:
> Lew wrote:
> > You might want to use standard library code for methods like
> > 'hexValue()' rather than rolling your own. I think API calls would help
> > 'readNumber()', too, but I'm not certain.

>
> I considered using Integer.parseInt(String, radix) for hexValue() but
> parseInt() wants a String, and i [sic] have an int. I dont want to create a
> String, just for invoking parseInt().
>


Wha...?

Why would you use 'parseInt()'? That doesn't even match what you're
trying to do. You want to go from 'int' to 'String', right?
'parseInt()' goes from 'String' to 'int', exactly the opposite. Seems
like a strange choice to consider.

It sounds like you need to read the API docs. Why don't you do that?

http://download.oracle.com/javase/6/...va.lang.String,
int)

> The readNumber() part is not yet done. I think NumberFormat.parse()
> would be suitable.
>


>> It's good, clean code except I am a bit puzzled by the presence of
>> private instance methods but the absence of public instance methods.
>> It's not bad, I suppose, but I'm not used to that.

>


> Most of the private methods have only one purpose: Improve readability.
> The name says what is done and encapsulates the implementation. Thus the
> parse() method is much easier to read.
>


That has nothing to do with my comment, though.

I was wondering why those were instance methods when their use is only
in a static context.

That aspect actually reduces readability.

--
Lew
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      03-03-2011
Lew wrote:
> http://download.oracle.com/javase/6/...Integer.html#v...,
> int)
>


static Integer valueOf(String s, int radix)

--
Lew
 
Reply With Quote
 
Joachim Lippold
Guest
Posts: n/a
 
      03-03-2011
Am 03.03.2011 20:04, Lew wrote:
> Joachim Lippold<(E-Mail Removed)> wrote:
>> Lew wrote:
>>> You might want to use standard library code for methods like
>>> 'hexValue()' rather than rolling your own. I think API calls would help
>>> 'readNumber()', too, but I'm not certain.

>>
>> I considered using Integer.parseInt(String, radix) for hexValue() but
>> parseInt() wants a String, and i [sic] have an int. I dont want to create a
>> String, just for invoking parseInt().
>>

>
> Wha...?
>
> Why would you use 'parseInt()'? That doesn't even match what you're
> trying to do. You want to go from 'int' to 'String', right?


No, i have to go from hex code character['0'-'9','a'-'f','A'-F'](stored
as int) to int[0-15]

> 'parseInt()' goes from 'String' to 'int', exactly the opposite. Seems
> like a strange choice to consider.
>
> It sounds like you need to read the API docs. Why don't you do that?
>
> http://download.oracle.com/javase/6/...va.lang.String,
> int)
>


That actually does the same thing as Integer.parseInt(String,int),
except it gives me an instance of Integer instead of an int.

Did you read your own link? It says:
[..]
In other words, this method returns an Integer object equal to the value of:
new Integer(Integer.parseInt(s, radix))
[..]

The cause why i am using neither is:
a) my source format is (char) int
b) Both Integer.parseInt(String, int) and Integer.valueOf(String, int),
require String as input.

So i would have to use:
inv value = Integer.parseInt(Character.toString('C'),16);

This creates an unneccessary instance of String. Why should i do that?

>>> It's good, clean code except I am a bit puzzled by the presence of
>>> private instance methods but the absence of public instance methods.
>>> It's not bad, I suppose, but I'm not used to that.

>>

>
>> Most of the private methods have only one purpose: Improve readability.
>> The name says what is done and encapsulates the implementation. Thus the
>> parse() method is much easier to read.
>>

>
> That has nothing to do with my comment, though.
>
> I was wondering why those were instance methods when their use is only
> in a static context.
>
> That aspect actually reduces readability.


There is a simple reason for that. Allmost all instance methods use
global variables like the JSONHandler, the Stringbuffer, the Stack and
the int.
Each parsing process is started staticly and creates his own private
context (an instance of JSONParser). This makes the parsing threadsafe.
If all these methods were static, parsing would no longer be threadsafe,
as the internal parser state would have to be static too.

Joe
 
Reply With Quote
 
Joachim Lippold
Guest
Posts: n/a
 
      03-03-2011
Am 03.03.2011 23:51, Joachim Lippold wrote:
>
> No, i have to go from hex code character['0'-'9','a'-'f','A'-F'](stored
> as int) to int[0-15]
>


However, i could have used Character.digit(int,int) instead of my
hexValue(int)
http://download.oracle.com/javase/6/...8int,%20int%29

Joe
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Lib to generate XML/JSON[P] output from a DTD/XSD/JSON Schema/etc Accio Centeno Python 1 02-15-2013 07:34 AM
I am facing an issue while decoding json string using json.loads sajuptpm Python 2 12-28-2012 07:16 AM
[ANN] Security Fix json-1.1.7 for json_pure and json gems Florian Frank Ruby 0 06-30-2009 05:18 PM
"JSON for ASP" at json.org Turul Topuz ASP General 1 06-27-2008 11:37 PM



Advertisments