streaming json parser for j2me?

Discussion in 'Java' started by Antti Järvinen, Mar 2, 2011.

  1. 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
     
    Antti Järvinen, Mar 2, 2011
    #1
    1. Advertising

  2. 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
     
    Joachim Lippold, Mar 3, 2011
    #2
    1. Advertising

  3. Antti Järvinen

    Lew Guest

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

    --
    Lew
    Honi soit qui mal y pense.
     
    Lew, Mar 3, 2011
    #3
  4. 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/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
     
    Joachim Lippold, Mar 3, 2011
    #4
  5. Antti Järvinen

    Paul Cager Guest

    On Mar 3, 1:28 pm, Lew <> wrote:
    > 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. Perhapsyou can
    > > build on this one:

    >
    > >http://bazaar.launchpad.net/~st-cdt/streamtastic/streamtastic-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.
     
    Paul Cager, Mar 3, 2011
    #5
  6. Antti Järvinen

    Lew Guest

    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!
     
    Lew, Mar 3, 2011
    #6
  7. Antti Järvinen

    Lew Guest

    Joachim Lippold <> 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/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.
    >


    >> 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
     
    Lew, Mar 3, 2011
    #7
  8. Antti Järvinen

    Lew Guest

    Lew, Mar 3, 2011
    #8
  9. Am 03.03.2011 20:04, Lew wrote:
    > Joachim Lippold<> 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/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?

    >>> 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
     
    Joachim Lippold, Mar 3, 2011
    #9
  10. Joachim Lippold, Mar 3, 2011
    #10
  11. Antti Järvinen, Mar 4, 2011
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Florian Frank
    Replies:
    0
    Views:
    244
    Florian Frank
    Jun 30, 2009
  2. sajuptpm
    Replies:
    2
    Views:
    345
    sajuptpm
    Dec 28, 2012
  3. Acácio Centeno
    Replies:
    1
    Views:
    258
    dieter
    Feb 15, 2013
  4. Bryan Britten
    Replies:
    9
    Views:
    264
    Bryan Britten
    May 28, 2013
  5. David Karr
    Replies:
    1
    Views:
    169
    David Karr
    Jun 17, 2013
Loading...

Share This Page