streaming json parser for j2me?

  • Thread starter Antti Järvinen
  • Start date
A

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??
 
J

Joachim Lippold

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??

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/streamtastic/streamtastic-icecast/files/head:/src/l/joe/json/

Regards Joe
 
L

Lew

Am 02.03.2011 15:10, schrieb Antti Järvinen:
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/streamtastic/streamtastic-icecast/files/head:/src/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.
 
J

Joachim Lippold

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/streamtastic/streamtastic-icecast/files/head:/src/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
 
P

Paul Cager

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. Perhapsyou can
build on this one:

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.
 
L

Lew

Paul said:
Lew said:
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.
 
L

Lew

Joachim Lippold said:
Lew said:
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/docs/api/java/lang/Integer.html#valueOf(java.lang.String,
int)
The readNumber() part is not yet done. I think NumberFormat.parse()
would be suitable.


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.
 
J

Joachim Lippold

Joachim Lippold said:
Lew said:
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/docs/api/java/lang/Integer.html#valueOf(java.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?
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top