suggestion to extend the JsonObject API

574 views
Skip to first unread message

Paulo Lopes

unread,
Jun 20, 2013, 3:40:18 AM6/20/13
to ve...@googlegroups.com
In vert.x JSON is the encoding of data passed between all verticles and modules. JSON comes from javascript and in Java it is encoded/decoded using the JsonObject/JsonArray/JsonElement classes.

Look at this example:

{
  "name": "Paulo",
  "id": 1,
  "link": null
}

This is a valid json, in JS you can query the fields like (assume the json above is assigned to the doc variable):

doc.name --> "Paulo"
doc.id --> 1
doc.link --> null
doc.homepage --> undefined (this is where i want to get...)

In java it would be something like:

doc.getField("name") --> "Paulo"
doc.getField("id") --> 1
doc.getField("link") --> null
doc.getField("homepage") --> null (java has no concept of undefined)

So how would one easily know if the field is really defined as null or that it does not exist at all?

I want to propose to add the extra function to the JsonObject.java class:

public boolean hasField(String fieldName) {
  return map.contains(fieldName);
}

This way in java i can test for "undefined" as jsonObject.hasField(someName);

I think this is extremely usefull and should be added, for JS there is no need to chance anything, for groovy since Json is being used as Map it already supports the contains method, for the remaining languages I don't know...

Tim Yates

unread,
Jun 20, 2013, 9:01:18 AM6/20/13
to ve...@googlegroups.com



--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Paulo Lopes

unread,
Jun 20, 2013, 9:07:20 AM6/20/13
to ve...@googlegroups.com
Yes i could but why do i would need to go to the implementation details of the json object in order to determine if a field is present or not?

That is the way i am using it but I think that there is no need to know the implementation details of the json object class and having a fieldExists method would make code more readable...

Tim Yates

unread,
Jun 20, 2013, 9:10:37 AM6/20/13
to ve...@googlegroups.com
I agree it would make your code more readable, it's just you implied in your original post that there was no way to do it in Java.

And I don't think calling a public method on a public class is a case of having to know the implementation details

Diego López León

unread,
Jun 20, 2013, 10:37:33 AM6/20/13
to ve...@googlegroups.com
I'm not quite sure we should replicate language concepts patching an
API. If Java doesn't have the notion of 'undefined' I see quite
reasonable that you must go into the object definition to simulate
your needs as you would do by checking the fieldNames set.
Even if the hasField is added, how the default value on the
get(String|Object|Array|Element|Boolean|Number|Long|Integer|Binary)
methods should behave? should them throw an exception if some field is
undefined? I think I see it way more confusing than just having to
make the set inspection.

My 2 μBTC
> --
> You received this message because you are subscribed to the Google Groups
> "vert.x" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to vertx+un...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>



--
diego

Paulo Lopes

unread,
Jun 20, 2013, 11:07:34 AM6/20/13
to ve...@googlegroups.com
Well on a java Map you can query for containsKey(keyName) which returns true if it exists or not and if you call get(nonExistingKey) you get null.

What i am proposing is not implement the undefined concept, i am proposing a way to verify if the field is present and that is not language specific since the Map interface implements it too...

pml...@gmail.com

unread,
Jun 20, 2013, 11:30:16 AM6/20/13
to ve...@googlegroups.com
Maybe it is not clear but the rationale behind this is that a non existing field is semantically not the same as a null value.


-----Original Message-----
From: Paulo Lopes
Sent: 20/06/2013, 17:07
To: ve...@googlegroups.com
Subject: Re: [vertx:9686] suggestion to extend the JsonObject API


Well on a java Map you can query for containsKey(keyName) which returns
true if it exists or not and if you call get(nonExistingKey) you get null.

What i am proposing is not implement the undefined concept, i am proposing
a way to verify if the field is present and that is not language specific
since the Map interface implements it too...

On Thursday, June 20, 2013 4:37:33 PM UTC+2, diega wrote:
>
> I'm not quite sure we should replicate language concepts patching an
> API. If Java doesn't have the notion of 'undefined' I see quite
> reasonable that you must go into the object definition to simulate
> your needs as you would do by checking the fieldNames set.
> Even if the hasField is added, how the default value on the
> get(String|Object|Array|Element|Boolean|Number|Long|Integer|Binary)
> methods should behave? should them throw an exception if some field is
> undefined? I think I see it way more confusing than just having to
> make the set inspection.
>
> My 2 μBTC
>
> On Thu, Jun 20, 2013 at 10:10 AM, Tim Yates <tim....@gmail.com<javascript:>>
> wrote:
> > I agree it would make your code more readable, it's just you implied in
> your
> > original post that there was no way to do it in Java.
> >
> > And I don't think calling a public method on a public class is a case of
> > having to know the implementation details
> >
> >
> > On 20 June 2013 14:07, Paulo Lopes <pml...@gmail.com <javascript:>>
> wrote:
> >>
> >> Yes i could but why do i would need to go to the implementation details
> of
> >> the json object in order to determine if a field is present or not?
> >>
> >> That is the way i am using it but I think that there is no need to know
> >> the implementation details of the json object class and having a
> fieldExists
> >> method would make code more readable...
> >>
> > --
> > You received this message because you are subscribed to the Google
> Groups
> > "vert.x" group.
> > To unsubscribe from this group and stop receiving emails from it, send
> an
> > email to vertx+un...@googlegroups.com <javascript:>.
> > For more options, visit https://groups.google.com/groups/opt_out.
> >
> >
>
>
>
> --
> diego
>

--
You received this message because you are subscribed to a topic in the Google Groups "vert.x" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vertx/mWkOomCJblI/unsubscribe.
To unsubscribe from this group and all of its topics, send an email to vertx+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages