Coder Social home page Coder Social logo

JsonObject.getArray VS Object[] about json-io HOT 2 CLOSED

jdereg avatar jdereg commented on August 26, 2024
JsonObject.getArray VS Object[]

from json-io.

Comments (2)

jdereg avatar jdereg commented on August 26, 2024

Recently, support for USE_MAPS was added to JsonReader.jsonToJava(). When USE_MAPS = true, the return value is a generic JsonObjects (Maps) where objects would be. The root of the JSON is the same as the original type of root object (e.g., string, double, long, null, etc. or Map if the root was an Object).

With this change, JsonReader.jsonToMaps() is no longer needed and will be deprecated. All jsonToMaps does now, is to ensure that the return value is always a Map. When this change was made, it did eliminate the need for the JsonObject.getArray() and JsonObject.isArray() methods in almost all cases. At this point, the only time you would use .getArray() or .isArray() would be if the root element of your object graph was an Collection type (ArrayList for example). In that case, in order to ensure that the right type is returned, the root element will have @type='arrayList' with "@Items":[item 1, item 2, ...].

In the example above, if you had called JsonReader.jsonToJava(json), then the ArrayList would be returned and no big deal. But, if you had called JsonReader.jsonToJava(json, [(JsonReader.USE_MAPS):true]), then a JsonObject would be returned that represents the root ArrayList object. You would call .getArray() to fetch it's items.

For brevity, I am using Groovy syntax for a Map of args above.

from json-io.

rednoah avatar rednoah commented on August 26, 2024

Thanks for the help. I understand how it works now. :)

from json-io.

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.