[jira] [Commented] (CONFIGURATION-686) JSONConfiguration doesn't support array of objects

2018-01-04 Thread Oliver Heger (JIRA)

[ 
https://issues.apache.org/jira/browse/CONFIGURATION-686?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16312013#comment-16312013
 ] 

Oliver Heger commented on CONFIGURATION-686:


Thanks for the confirmation.

> JSONConfiguration doesn't support array of objects
> --
>
> Key: CONFIGURATION-686
> URL: https://issues.apache.org/jira/browse/CONFIGURATION-686
> Project: Commons Configuration
>  Issue Type: Bug
>Affects Versions: 2.2
>Reporter: Vincent Maurin
> Fix For: 2.3
>
>
> Hello,
> I have noticed that the array type is not properly handled by the 
> JSONConfiguration (or AbstractYAMLBasedConfiguration)
> If we take the example from json.org
> {noformat}
> {
>   "menu": {
> "id": "file",
> "value": "File",
> "popup": {
>   "menuitem": [
> {
>   "value": "New",
>   "onclick": "CreateNewDoc()"
> },
> {
>   "value": "Open",
>   "onclick": "OpenDoc()"
> },
> {
>   "value": "Close",
>   "onclick": "CloseDoc()"
> }
>   ]
> }
>   }
> }
> {noformat}
> I would have expect the following call to return "New" 
> {code:java}
> configuration.getString("menu.popup.menuitem(0).value")
> {code}
> The issue, I guess, is that only the Map type is handled by 
> AbstractYAMLBasedConfiguration in a recursive way and not the list type.
> I had a custom JSONConfiguration implementation before supporting this use 
> case, sorry for the code, it could be a bit messy and old, but it is maybe 
> clearer to understand my issue
> {code:java}
> @Override
> @SuppressWarnings("unchecked")
> public void read(Reader in) throws ConfigurationException {
> try {
> Object jsonRoot = MAPPER.readValue(in, Object.class);
> List roots = createHierarchy(jsonRoot);
> if (roots.size() != 1) {
> throw new ConfigurationException("The configuration only 
> support a single root");
> }
> getSubConfigurationParentModel().mergeRoot(roots.get(0).create(), 
> null, null, null, null);
> } catch (IOException ex) {
> throw new ConfigurationException("Invalid JsonConfiguration", ex);
> }
> }
>  
> @SuppressWarnings("unchecked")
> private static List createHierarchy(Object value) {
> if (value instanceof Map) {
> ImmutableNode.Builder result = new ImmutableNode.Builder();
> Map map = (Map) value;
> for (Map.Entry entry : map.entrySet()) {
> List children = 
> createHierarchy(entry.getValue());
> for (ImmutableNode.Builder child : children) {
> child.name(entry.getKey());
> result.addChild(child.create());
> }
> }
> return Collections.singletonList(result);
> } else if (value instanceof List) {
> List list = (List) value;
> List result = new ArrayList<>(list.size());
> for (Object item : list) {
> List children = createHierarchy(item);
> for (ImmutableNode.Builder child : children) {
> result.add(child);
> }
> }
> return result;
> }
> ImmutableNode.Builder result = new ImmutableNode.Builder();
> result.value(value);
> return Collections.singletonList(result);
> }
> {code}
>  



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)


[jira] [Commented] (CONFIGURATION-686) JSONConfiguration doesn't support array of objects

2018-01-04 Thread Vincent Maurin (JIRA)

[ 
https://issues.apache.org/jira/browse/CONFIGURATION-686?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16311033#comment-16311033
 ] 

Vincent Maurin commented on CONFIGURATION-686:
--

Thank you for the quick patch [~oliver.he...@t-online.de].

My tests are working now with the revision 1819881 so I confirm it is working 
as expected.

> JSONConfiguration doesn't support array of objects
> --
>
> Key: CONFIGURATION-686
> URL: https://issues.apache.org/jira/browse/CONFIGURATION-686
> Project: Commons Configuration
>  Issue Type: Bug
>Affects Versions: 2.2
>Reporter: Vincent Maurin
> Fix For: 2.3
>
>
> Hello,
> I have noticed that the array type is not properly handled by the 
> JSONConfiguration (or AbstractYAMLBasedConfiguration)
> If we take the example from json.org
> {noformat}
> {
>   "menu": {
> "id": "file",
> "value": "File",
> "popup": {
>   "menuitem": [
> {
>   "value": "New",
>   "onclick": "CreateNewDoc()"
> },
> {
>   "value": "Open",
>   "onclick": "OpenDoc()"
> },
> {
>   "value": "Close",
>   "onclick": "CloseDoc()"
> }
>   ]
> }
>   }
> }
> {noformat}
> I would have expect the following call to return "New" 
> {code:java}
> configuration.getString("menu.popup.menuitem(0).value")
> {code}
> The issue, I guess, is that only the Map type is handled by 
> AbstractYAMLBasedConfiguration in a recursive way and not the list type.
> I had a custom JSONConfiguration implementation before supporting this use 
> case, sorry for the code, it could be a bit messy and old, but it is maybe 
> clearer to understand my issue
> {code:java}
> @Override
> @SuppressWarnings("unchecked")
> public void read(Reader in) throws ConfigurationException {
> try {
> Object jsonRoot = MAPPER.readValue(in, Object.class);
> List roots = createHierarchy(jsonRoot);
> if (roots.size() != 1) {
> throw new ConfigurationException("The configuration only 
> support a single root");
> }
> getSubConfigurationParentModel().mergeRoot(roots.get(0).create(), 
> null, null, null, null);
> } catch (IOException ex) {
> throw new ConfigurationException("Invalid JsonConfiguration", ex);
> }
> }
>  
> @SuppressWarnings("unchecked")
> private static List createHierarchy(Object value) {
> if (value instanceof Map) {
> ImmutableNode.Builder result = new ImmutableNode.Builder();
> Map map = (Map) value;
> for (Map.Entry entry : map.entrySet()) {
> List children = 
> createHierarchy(entry.getValue());
> for (ImmutableNode.Builder child : children) {
> child.name(entry.getKey());
> result.addChild(child.create());
> }
> }
> return Collections.singletonList(result);
> } else if (value instanceof List) {
> List list = (List) value;
> List result = new ArrayList<>(list.size());
> for (Object item : list) {
> List children = createHierarchy(item);
> for (ImmutableNode.Builder child : children) {
> result.add(child);
> }
> }
> return result;
> }
> ImmutableNode.Builder result = new ImmutableNode.Builder();
> result.value(value);
> return Collections.singletonList(result);
> }
> {code}
>  



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)


[jira] [Commented] (CONFIGURATION-686) JSONConfiguration doesn't support array of objects

2017-12-29 Thread Oliver Heger (JIRA)

[ 
https://issues.apache.org/jira/browse/CONFIGURATION-686?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16306243#comment-16306243
 ] 

Oliver Heger commented on CONFIGURATION-686:


Thank you for the report and the solution proposal. I will have a look 
(probably in a couple of days).

> JSONConfiguration doesn't support array of objects
> --
>
> Key: CONFIGURATION-686
> URL: https://issues.apache.org/jira/browse/CONFIGURATION-686
> Project: Commons Configuration
>  Issue Type: Bug
>Affects Versions: 2.2
>Reporter: Vincent Maurin
>
> Hello,
> I have noticed that the array type is not properly handled by the 
> JSONConfiguration (or AbstractYAMLBasedConfiguration)
> If we take the example from json.org
> {noformat}
> {
>   "menu": {
> "id": "file",
> "value": "File",
> "popup": {
>   "menuitem": [
> {
>   "value": "New",
>   "onclick": "CreateNewDoc()"
> },
> {
>   "value": "Open",
>   "onclick": "OpenDoc()"
> },
> {
>   "value": "Close",
>   "onclick": "CloseDoc()"
> }
>   ]
> }
>   }
> }
> {noformat}
> I would have expect the following call to return "New" 
> {code:java}
> configuration.getString("menu.popup.menuitem(0).value")
> {code}
> The issue, I guess, is that only the Map type is handled by 
> AbstractYAMLBasedConfiguration in a recursive way and not the list type.
> I had a custom JSONConfiguration implementation before supporting this use 
> case, sorry for the code, it could be a bit messy and old, but it is maybe 
> clearer to understand my issue
> {code:java}
> @Override
> @SuppressWarnings("unchecked")
> public void read(Reader in) throws ConfigurationException {
> try {
> Object jsonRoot = MAPPER.readValue(in, Object.class);
> List roots = createHierarchy(jsonRoot);
> if (roots.size() != 1) {
> throw new ConfigurationException("The configuration only 
> support a single root");
> }
> getSubConfigurationParentModel().mergeRoot(roots.get(0).create(), 
> null, null, null, null);
> } catch (IOException ex) {
> throw new ConfigurationException("Invalid JsonConfiguration", ex);
> }
> }
>  
> @SuppressWarnings("unchecked")
> private static List createHierarchy(Object value) {
> if (value instanceof Map) {
> ImmutableNode.Builder result = new ImmutableNode.Builder();
> Map map = (Map) value;
> for (Map.Entry entry : map.entrySet()) {
> List children = 
> createHierarchy(entry.getValue());
> for (ImmutableNode.Builder child : children) {
> child.name(entry.getKey());
> result.addChild(child.create());
> }
> }
> return Collections.singletonList(result);
> } else if (value instanceof List) {
> List list = (List) value;
> List result = new ArrayList<>(list.size());
> for (Object item : list) {
> List children = createHierarchy(item);
> for (ImmutableNode.Builder child : children) {
> result.add(child);
> }
> }
> return result;
> }
> ImmutableNode.Builder result = new ImmutableNode.Builder();
> result.value(value);
> return Collections.singletonList(result);
> }
> {code}
>  



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)