Value containing comma is treated as a delimiter even if it is escaped

5,207 views
Skip to first unread message

Madhav Bhargava

unread,
Nov 6, 2012, 12:37:31 PM11/6/12
to jcomm...@googlegroups.com
Hi All,

I have an argument which takes in a JSON string as a value. I have then written a converter which will parse this string and return a populated custom type. The problem that i am facing is that the Json object will always have a comma as a separator for its fields.

To take an example:
Json String value: [{key1:value, key2:value2}, {key1:value3, key2:value4}]
The problem is that the string that converter gets as value in the convert method argument is "[{key:value" rest is chopped off. Now i have tried escaping the comma but that does not work. I wonder why that has been done. It seriously restricts the usage of custom types and converters.

I would be glad if someone could suggest an alternative

Best Regards,
Madhav

Madhav Bhargava

unread,
Nov 6, 2012, 1:13:38 PM11/6/12
to jcomm...@googlegroups.com
Hi All,

After going through the JCommander source code i realize that if no listConverter is specified then a default CommaParameterSplitter is used.

The value of my parameter is a JsonArray and therefore the requirement is to parse that into a List<CustomType>. So seeing a List<T> as a return type JCommander assumes that a IParameterSplitter  needs to be specified. So something like this will not work:

@Parameter(names = { "-w", "--workers" }, required = false, description = "Json Array of the format: [ {\"t\":\"<tId>\", \"nType\":\"<nType>\", \"instances\":<no of instances>", converter = SomeConverter.class)
    private List<SomeType> workerJson;

The above will cause a problem.

Best Regards,
Madhav

Madhav Bhargava

unread,
Nov 6, 2012, 2:04:33 PM11/6/12
to jcomm...@googlegroups.com
Hi All,

I have now changed my code and i am successfully able to parse Json Arrays.
There is one problem however. While converting if I find that the Json Object is not valid i do not wish to add it. Since i have to return something from the converter i return null as i do not wish to throw an exception but i wish to only skip adding this converted value to the final list of values.

The problem is in JCommander#convertToList method:

for (String param : splitter.split(value)) {
      result.add(converter.convert(param));
}

This should in my opinion have a null check. ArrayList allows insertion of nulls. It accepts null as a valid value and therefore if you call List#size method then it will also count null which we do not want.

best Regards,
Madhav

On Tuesday, November 6, 2012 11:07:31 PM UTC+5:30, Madhav Bhargava wrote:

Cédric Beust ♔

unread,
Nov 6, 2012, 2:27:36 PM11/6/12
to jcomm...@googlegroups.com
Hi Madhav,

The expected behavior here is that the converter should throw if it can't convert the value...

-- 
Cédric

Tyler Jewell

unread,
Jan 2, 2014, 4:44:59 PM1/2/14
to jcomm...@googlegroups.com
Hi Madhav:

Could you kindly post your code that allowed you to bypass the default Comma Conversion of Strings with JSON objects?  I am running into the same issue you are.  But when I implement my own Converter, the string that is passed in for conversion has already had the comma separation applied.  I haven't figured out how to get the raw string input from the argument using JCommander.

Tyler

Tyler Jewell

unread,
Jan 2, 2014, 5:08:57 PM1/2/14
to jcomm...@googlegroups.com
And, as soon as I wrote this post, I now have a valid answer for it.

The solution was to add listConverter = JSONPairConverter.class to my @Parameter designation.   JSONPairConverter is a custom class that I wrote to do this conversion.

FWIW, I tried setting the splitter field, as the documentation implied that a comma separated splitter was the default.  But it didn't work.


Tyler Jewell -- ceo @codenvy.com -- +1 978-884-5355 -- Free/Busy Times 


--
You received this message because you are subscribed to a topic in the Google Groups "jcommander" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jcommander/g_bXIyGyHUE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jcommander+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages