GithubHelp home page GithubHelp logo

Duplicate key processing? about json-java HOT 5 CLOSED

XIAYM-gh avatar XIAYM-gh commented on June 8, 2024
Duplicate key processing?

from json-java.

Comments (5)

stleary avatar stleary commented on June 8, 2024

@XIAYM-gh Thanks for submitting this issue. While the spec does not seem to disallow duplicate object keys, I am not sure how it would work in JSON-Java. For example, what should JSONObject do with a duplicate key, since it cannot be stored as-is?
If implemented, it will need to be opt-in using JSONParserConfiguration to capture the option. That class is implemented in #805, which is stalled at the moment.
Suggest that you try forking the project, implementing non-unique key support in a way that makes sense to you, and get an idea of how it might affect the rest of the library.

from json-java.

XIAYM-gh avatar XIAYM-gh commented on June 8, 2024

Thanks for replying, I will take a look at it.
but because I'm a senior high student in China mainland, it's hard for me to do that until the 30th day of this month
sorry

from json-java.

XIAYM-gh avatar XIAYM-gh commented on June 8, 2024

So, I've implemented 4 handling methods in my PR and here are them:

  • THROW_EXCEPTION, it's the original way to handle duplicate keys
  • OVERWRITE and IGNORE, they are quite useful actually
  • MERGE_INTO_ARRAY, this is the default method that the json-lib use, and it may be useful sometimes for somebody

What do you think about them?

from json-java.

stleary avatar stleary commented on June 8, 2024

@XIAYM-gh Thanks for posting.

For context, here is what rfc-8259 says about objects:

`An object structure is represented as a pair of curly brackets
surrounding zero or more name/value pairs (or members). A name is a
string. A single colon comes after each name, separating the name
from the value. A single comma separates a value from a following
name. The names within an object SHOULD be unique.

  object = begin-object [ member *( value-separator member ) ]
           end-object

  member = string name-separator value

An object whose names are all unique is interoperable in the sense
that all software implementations receiving that object will agree on
the name-value mappings. When the names within an object are not
unique, the behavior of software that receives such an object is
unpredictable. Many implementations report the last name/value pair
only. Other implementations report an error or fail to parse the
object, and some implementations report all of the name/value pairs,
including duplicates.

JSON parsing libraries have been observed to differ as to whether or
not they make the ordering of object members visible to calling
software. Implementations whose behavior does not depend on member
ordering will be interoperable in the sense that they will not be
affected by these differences.`

The JSON-Java object representation is fixed as an unordered map with unique keys in this
implementation. That will not be changed, so ignore is not an option.
If someone has a reasonable use case for merge_into_array, feel free to
post a comment, otherwise it should probably be omitted as well.
That leaves throw_exception (the default behavior), and overwrite.

No objection if you want to try adding overwrite as an opt-in behavior in JSONParserConfiguration
or ParserConfiguration. It is probably not necessary to use JsonDuplicateKeyStrategy.

from json-java.

XIAYM-gh avatar XIAYM-gh commented on June 8, 2024

Oh thanks, I will do it as soon as possible

from json-java.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.