org.kopitubruk.util.json
Interface JSONAble

All Known Implementing Classes:
JsonObject

public interface JSONAble

This interface specifies a method which implementing objects will use to provide a JSON representation of themselves. When JSONUtil.toJSON(Object, JSONConfig) encounters one of these objects as one of its property values, it will call this method in those objects to convert them to JSON.

The Java 8 version of this interface includes default versions of 3 of the 4 methods and is a functional interface allowing its use in Java 8 Lambda expressions. Java 7 and earlier do not support default methods in interfaces or functional interfaces so those features have been removed from the Java 7 and earlier versions of this interface.

Author:
Bill Davidson

Method Summary
 String toJSON()
          Convert this object to a string of JSON data.
 String toJSON(JSONConfig jsonConfig)
          Convert this object to a string of JSON data.
 void toJSON(JSONConfig jsonConfig, Writer json)
          Write this object to the given writer as JSON data.
 void toJSON(Writer json)
          Write to the given writer as JSON data using all defaults for the configuration.
 

Method Detail

toJSON

String toJSON()
Convert this object to a string of JSON data.

This version does not use a JSONConfig and so can break loop detection. In many common cases, that's not a problem and this is just more convenient.

The default implementation calls toJSON(JSONConfig, Writer) to actually do the work.

Returns:
A string of JSON data.

toJSON

String toJSON(JSONConfig jsonConfig)
Convert this object to a string of JSON data.

Implementations which use JSONUtil to generate their JSON should pass the supplied JSONConfig to JSONUtil.toJSON(Object, JSONConfig) or JSONUtil.toJSON(Object, JSONConfig, Writer) in order to preserve data structure loop detection. If this is not done, then loops may not be detected properly.

The default implementation calls toJSON(JSONConfig, Writer) to actually do the work.

Parameters:
jsonConfig - A configuration object to use to optionally set encoding options.
Returns:
A string of JSON data.

toJSON

void toJSON(Writer json)
            throws IOException
Write to the given writer as JSON data using all defaults for the configuration.

The default implementation calls toJSON(JSONConfig, Writer) to actually do the work.

Parameters:
json - json A writer for the output.
Throws:
IOException - If there is an error on output.

toJSON

void toJSON(JSONConfig jsonConfig,
            Writer json)
            throws IOException
Write this object to the given writer as JSON data. This is the one that gets called by JSONUtil's toJSON methods so if you include JSONAbles inside other structures that you will be passing to JSONUtil's toJSON methods then this one really needs to be implemented and not just "stubbed".

Implementations which use JSONUtil to generate their JSON should pass the supplied JSONConfig to JSONUtil.toJSON(Object, JSONConfig) or JSONUtil.toJSON(Object, JSONConfig, Writer) in order to preserve data structure loop detection. If this is not done, then loops may not be detected properly.

Parameters:
jsonConfig - A configuration object to use to optionally set encoding options.
json - A writer for the output.
Throws:
IOException - If there is an error on output.


Copyright © 2016. All rights reserved.