GithubHelp home page GithubHelp logo

reuschling / json-io Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jdereg/json-io

0.0 1.0 0.0 3.05 MB

Perfect Java serialization to and from JSON format

License: Apache License 2.0

JavaScript 1.00% Java 43.62% Groovy 55.38%

json-io's Introduction

json-io

Maven Central Javadoc

Perfect Java serialization to and from JSON format (available on Maven Central). To include in your project:

<dependency>
  <groupId>com.cedarsoftware</groupId>
  <artifactId>json-io</artifactId>
  <version>4.14.1</version>
</dependency>

json-io consists of two main classes, a reader (JsonReader) and a writer (JsonWriter). json-io eliminates the need for using ObjectInputStream / ObjectOutputStream to serialize Java and instead uses the JSON format.

json-io does not require that Java classes implement Serializable or Externalizable to be serialized, unlike the JDK's ObjectInputStream / ObjectOutputStream. It will serialize any Java object graph into JSON and retain complete graph semantics / shape and object types. This includes supporting private fields, private inner classes (static or non-static), of any depth. It also includes handling cyclic references. Objects do not need to have public constructors to be serialized. The output JSON will not include transient fields, identical to the ObjectOutputStream behavior.

json-io does not depend on any 3rd party libraries, has extensive support for Java Generics, and allows extensive customization.

A few advantages of json-io over Google's gson library:

  • gson will fail with infinite recursion (StackOverflowError) when there is a cycle in the input data. Illustrated here.
  • gson cannot handle non-static inner classes. Illustrated here.
  • gson cannot handle hetereogeneous Collections, Object[], or Maps. Illustrated here.
  • gson cannot handle Maps with keys that are not Strings. Illustrated here.

Format

json-io uses proper JSON format. As little type information is included in the JSON format to keep it compact as possible. When an object's class can be inferred from a field type or array type, the object's type information is left out of the stream. For example, a String[] looks like ["abc", "xyz"].

When an object's type must be emitted, it is emitted as a meta-object field "@type":"package.class" in the object.
When read, this tells the JsonReader what class to instantiate. (@type output can be turned off - see User Guide).

If an object is referenced more than once, or references an object that has not yet been defined, (say A points to B, and B points to C, and C points to A), it emits a "@ref":n where 'n' is the object's integer identity (with a corresponding meta entry "@id":n defined on the referenced object). Only referenced objects have IDs in the JSON output, reducing the JSON String length.

Performance

json-io was written with performance in mind. In most cases json-io is faster than the JDK's ObjectInputStream / ObjectOutputStream. As the tests run, a log is written of the time it takes to serialize / deserialize and compares it to ObjectInputStream / ObjectOutputStream (if the static variable _debug is true in TestUtil).

Pretty-Printing JSON

Use JsonWriter.formatJson() API to format a passed in JSON string to a nice, human readable format. Also, when writing JSON data, use the JsonWriter.objectToJson(o, args) API, where args is a Map with a key of JsonWriter.PRETTY_PRINT and a value of 'true' (boolean or String). When run this way, the JSON written by the JsonWriter will be formatted in a nice, human readable format.

RESTful support

json-io can be used as the fundamental data transfer method between a Javascript / JQuery / Ajax client and a web server in a RESTful fashion.

See json-command-servlet for a light-weight servlet that processes REST requests.

Noteworthy

For useful Java utilities, check out java-util

Featured on json.org.

Revision History


Sponsors

Alt text

YourKit supports open source projects with its full-featured Java Profiler. YourKit, LLC is the creator of YourKit Java Profiler and YourKit .NET Profiler, innovative and intelligent tools for profiling Java and .NET applications.

Intellij IDEA from JetBrains Intellij IDEA


License

Copyright (c) 2007 Cedar Software LLC.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

by John DeRegnaucourt

json-io's People

Contributors

bmbanker avatar darabos avatar darmbrust avatar daveloyall avatar dependabot[bot] avatar devlynnx avatar dtracers avatar frabert avatar h143570 avatar jdereg avatar jsnyder4 avatar kaihufenbach avatar kpartlow avatar lance0428 avatar laurgarn avatar lightcycle avatar lordvlad avatar mhmxs avatar ozhelezniak-talend avatar pedrocpneto avatar pwntester avatar reuschling avatar senich avatar sgandon avatar thomasx avatar twillouer avatar wwang-talend avatar

Watchers

 avatar

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.