Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve XML to JSON convert logic #243

Open
greenlaw110 opened this issue Dec 28, 2020 · 1 comment
Open

Improve XML to JSON convert logic #243

greenlaw110 opened this issue Dec 28, 2020 · 1 comment
Assignees

Comments

@greenlaw110
Copy link
Collaborator

greenlaw110 commented Dec 28, 2020

The introducing of #240 brought in an error that it cannot handle simple XML without attributes conversion correctly.

This CR is to improve XML to JSON convert logic to handle XML with or without attributes correctly.

Case 1.1 - simple XML without attributes

<root>
<parent>
  <child>123</child>
</parent>
</root>

converted ($.convert(xmlDoc).to(JSONObject.class)) into

{
  "parent": {
    "child": 123
  }
}

Case 1.2 - simple XML list without attributes

<root>
<parent>
  <child>123</child>
</parent>
<parent>
  <child>456</child>
</parent>
</root>

converted into

JSON without MERGE_MAP hint:

{
  "parent": [
    {
      "child": 123
    },
    {
      "child": 456
    }
  ]
}

JSON with MERGE_MAP hint ($.convert(xmlDoc).hint(XmlToJson.HINT_MERGE_MAP).to(JSONObject.class)) turned on:

{
  "parent": {
    "child": [123, 456]
  }
}

Case 1.3 - simple mixed XML list without attributes

<root>
<parent>
  <child>123</child>
</parent>
<parent>
  <child>456</child>
</parent>
<parent2>
  <child>789</child>
</parent2>
<someone>abc</someone>
</root>

converted into

JSON without MERGE_MAP hint:

{
  "parent": [
    {
      "child": 123
    },
    {
      "child": 456
    }
  ],
  "parent2": {
    "child": 789
  },
  "someone": "abc"
}

JSON with MERGE_MAP hint turned on

{
  "parent": {
    "child": [123, 456]
  },
  "parent2": {
    "child": 789
  },
  "someone": "abc"
}

Case 2.1 - XML with attributes and no text

<root>
<parent>
  <child value="123"/>
</parent>
</root>

converted into

{
  "parent": {
    "child": {
       "value": 123
    }
  }
}

Case three - XML with attributes and value

<root>
<parent>
  <child value="123">abc</child>
</parent>
</root>

converted into

{
  "parent": {
    "child": {
       "value": 123,
       "innerValue": "abc"
    }
  }
}

Note

OSGL tool XML to JSON convert tool require at least one ELEMENT type node inside the root node, otherwise it returns an empty JSONObject.

E.g. converting the following xml document will return an empty JSONObject:

<root>abc</root>
@greenlaw110 greenlaw110 self-assigned this Dec 28, 2020
greenlaw110 pushed a commit that referenced this issue Jan 1, 2021
greenlaw110 pushed a commit that referenced this issue Jan 1, 2021
@greenlaw110
Copy link
Collaborator Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant