[RFC] Implement custom iterator for JSONArray to ensure fetched values are JSON compliant

Open
#673 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Bug
Clarity
Needs clarification
Activity status
Stale
Tech stack
java
Domain
backend

Research direction

Start with JSONArray.iterator and review the related behavior in JSONObject, JSONArray put* operations, and toString methods; issue #672 provides the referenced context. Decide between lazy iterator wrapping and proactive wrapping during insertion, then verify that code-built values exposed to JSON processors are compliant without changing parsed objects.

Written by the indexing model from the issue text.

Description

Active discussion Fix before the next release

This has been a "known" issue for some time, but as mentioned in #672, the JSONArray.iterator method is exposing the raw values from the backing array instead of JSON-compliant values.

We should do 1 of 2 things:

  1. Implement a private/internal JSONArrayIterator class that would ensure any fetched values are properly "wrapped" and thus suitable output to JSON processors.
  2. Update all of our put* operations to ensure that all values are properly "wrapped" in both JSONObject and JSONArray. This would also allow us to simplify our toString methods and remove the wrapping from there as all values should be proper JSON.

Option 1 keeps the wrapping "lazy" and thus would only guarantee JSON compliance on values that are fetched. Fetching a non-wrapped value multiple times would cause wrapping to happen each time.

Option 2 makes the wrapping more proactive; guaranteeing that our JSON compliance happens up-front even if the value is not fetched later. Fetching an initially non-wrapped value multiple times would cause NO wrapping to happen at fetch as it would have been pre-wrapped by the put operation.

Both options only affect code-built JSONObjects/Arrays. Parsed objects would be unaffected as the parser is only placing JSON-compliant values in the backing collections.

Thoughts on whether we should implement either option, something else, or neither?

Dominant language
Java
Stars
4.7k
Forks
2.6k
Avg merge
11d 18m
Merged PRs (30d)
1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from stleary/JSON-java

All issues in stleary/JSON-java

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.