-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Open
Description
Hey folks!
When we use forceList and there happens to be either an empty tag or an empty element in the middle or the end of the array, the conversion resets the context object without accounting the presence of a previously collected array state.
Empty in the end
final String input = "<root><id>1</id><id/></root>"; //or ending with a <id></id>
HashSet<String> forceListCandidates = new HashSet<>();
forceListCandidates.add("id");
final JSONObject json = XML.toJSONObject(input,
new XMLParserConfiguration()
.withKeepStrings(false)
.withcDataTagName("content")
.withForceList(forceListCandidates)
.withConvertNilAttributeToNull(true));Output:
{"root":{"id":[]}}Empty in the middle
final String input = "<root><id>1</id><id/><id>2</id></root>"; //or <id></id> in the middle
HashSet<String> forceListCandidates = new HashSet<>();
forceListCandidates.add("id");
final JSONObject json = XML.toJSONObject(input,
new XMLParserConfiguration()
.withKeepStrings(false)
.withcDataTagName("content")
.withForceList(forceListCandidates)
.withConvertNilAttributeToNull(true));Output:
{"root":{"id":[2]}}Code snippet responsible for empty element

Code snippet responsible for empty tag
I've done the fixes on my local branch so if you don't mind I'm ready to pick this up after we've gone through this together!
Thanks!
Metadata
Metadata
Assignees
Labels
No labels