com.h3c.util.json

## Class AbstractJsonCodec<T>

- java.lang.Object
- - com.h3c.util.json.AbstractJsonCodec<T>

- Type Parameters:
    `T` - typed class of the POJO for which this JsonCodec serves
  All Implemented Interfaces:
    JsonCodec<T>
  Direct Known Subclasses:
    AbstractMetricDescriptorCodec, AlertCodec, AlertTopicCodec, AlertTopicListenerCodec, ARPEntryCodec, AuditLogCodec, ConfigCodec, ControllerStatsCodec, CounterMetricCodec, DataPathInfoCodec, DpSuppressedPortsCodec, GaugeMetricCodec, HistogramMetricCodec, LicenseCodec, LinkCodec, MeterMetricCodec, MetricUpdateCodec, NetworkNodeCodec, OfJsonCodec, OfmExperimenterCodec, RatioGaugeMetricCodec, RegionCodec, ReportContentCodec, RollingCounterMetricCodec, SystemInformationCodec, TimerMetricCodec, TopologyClusterCodec, TopologyPathCodec

      public abstract class AbstractJsonCodec<T>
      extends Object
      implements JsonCodec<T>

  An abstract JsonCodec. Sub-classes of this class should implement a strongly-typed `JsonCodec` interface.

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected static org.slf4j.Logger` | `log`
    `static com.fasterxml.jackson.databind.ObjectMapper` | `mapper` A JSON Mapper used for JSON encoding/decoding.
    `protected static String` | `NO_DECODING_SUP`
    `protected String` | `root`
    `protected String` | `roots`

  - ### Constructor Summary

    Constructors
    Modifier | Constructor and Description
    --- | ---
    `protected` | `AbstractJsonCodec(String root, String roots)`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected com.fasterxml.jackson.databind.node.ArrayNode` | `arrayNode()`
    `T` | `decode(String json)` Decodes the JSON string into a typed POJO.
    `List<T>` | `decodeList(String json)` Decodes the JSON string into a typed list of POJO.
    `String` | `encode(T pojo, boolean prettyPrint)` Encodes the given POJO to its JSON string representation.
    `com.fasterxml.jackson.databind.node.ArrayNode` | `encodeList(Collection<T> pojos)` Encodes the collection of POJOs to their JSON representation.
    `String` | `encodeList(Collection<T> pojos, boolean prettyPrint)` Encodes the collection of POJOs to their JSON string representation.
    `protected com.fasterxml.jackson.databind.JsonNode` | `fromEnums(Set<? extends Enum<?>> enums)`
    `protected boolean` | `isMissingContent(com.fasterxml.jackson.databind.JsonNode node, String... keys)` Checks for missing keys in a node.
    `protected static long` | `longVal(com.fasterxml.jackson.databind.JsonNode node, String name)`
    `protected com.fasterxml.jackson.databind.node.ObjectNode` | `objectNode()`
    `protected com.fasterxml.jackson.databind.JsonNode` | `read(String json)`
    `String` | `root()` Returns the root element's singular name.
    `String` | `roots()` Returns the root element's plural name.
    `protected <E extends Enum<E>> Set<E>` | `toEnums(com.fasterxml.jackson.databind.node.ArrayNode ar, Class<E> enumClazz)`
    `protected String` | `write(com.fasterxml.jackson.databind.JsonNode node, boolean prettyPrint)`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

    - ### Methods inherited from interface com.h3c.util.json.JsonCodec

      `decode, encode`

- - ### Field Detail

    - #### log

          protected static final org.slf4j.Logger log

    - #### mapper

          public static final com.fasterxml.jackson.databind.ObjectMapper mapper

      A JSON Mapper used for JSON encoding/decoding.

    - #### NO_DECODING_SUP

          protected static final String NO_DECODING_SUP

      See Also:
        Constant Field Values

    - #### root

          protected final String root

    - #### roots

          protected final String roots

  - ### Constructor Detail

    - #### AbstractJsonCodec

          protected AbstractJsonCodec(String root,
                           String roots)

  - ### Method Detail

    - #### root

          public String root()

      Description copied from interface: `JsonCodec`
      Returns the root element's singular name.
      Specified by:
        `root` in interface `JsonCodec<T>`
      Returns:
        the root element's singular name

    - #### roots

          public String roots()

      Description copied from interface: `JsonCodec`
      Returns the root element's plural name.
      Specified by:
        `roots` in interface `JsonCodec<T>`
      Returns:
        the root element's plural name

    - #### encode

          public String encode(T pojo,
                      boolean prettyPrint)

      Description copied from interface: `JsonCodec`
      Encodes the given POJO to its JSON string representation.
      Specified by:
        `encode` in interface `JsonCodec<T>`
      Parameters:
        `pojo` - POJO to encode
        `prettyPrint` - Human-readable formatted JSON if true, single-line JSON otherwise
      Returns:
        JSON string representation of POJO

    - #### encodeList

          public String encodeList(Collection<T> pojos,
                          boolean prettyPrint)

      Description copied from interface: `JsonCodec`
      Encodes the collection of POJOs to their JSON string representation.
      Specified by:
        `encodeList` in interface `JsonCodec<T>`
      Parameters:
        `pojos` - list of POJO's to encode
        `prettyPrint` - human-readable formatted JSON if true, single-line JSON otherwise
      Returns:
        JSON string representation of the list of POJO's

    - #### encodeList

          public com.fasterxml.jackson.databind.node.ArrayNode encodeList(Collection<T> pojos)

      Description copied from interface: `JsonCodec`
      Encodes the collection of POJOs to their JSON representation.
      Specified by:
        `encodeList` in interface `JsonCodec<T>`
      Parameters:
        `pojos` - list of POJO's to encode
      Returns:
        ArrayNode representation of the list of POJO's. This does not have the "top" root

    - #### decode

          public T decode(String json)

      Description copied from interface: `JsonCodec`
      Decodes the JSON string into a typed POJO.
      Specified by:
        `decode` in interface `JsonCodec<T>`
      Parameters:
        `json` - JSON string representation of the typed POJO
      Returns:
        typed POJO

    - #### decodeList

          public List<T> decodeList(String json)

      Description copied from interface: `JsonCodec`
      Decodes the JSON string into a typed list of POJO.
      Specified by:
        `decodeList` in interface `JsonCodec<T>`
      Parameters:
        `json` - JSON string representation of the typed list of POJOs
      Returns:
        typed list of POJOs

    - #### fromEnums

          protected com.fasterxml.jackson.databind.JsonNode fromEnums(Set<? extends Enum<?>> enums)

    - #### toEnums

          protected <E extends Enum<E>> Set<E> toEnums(com.fasterxml.jackson.databind.node.ArrayNode ar,
                                           Class<E> enumClazz)

    - #### write

          protected String write(com.fasterxml.jackson.databind.JsonNode node,
                     boolean prettyPrint)

    - #### read

          protected com.fasterxml.jackson.databind.JsonNode read(String json)

    - #### isMissingContent

          protected boolean isMissingContent(com.fasterxml.jackson.databind.JsonNode node,
                                 String... keys)

      Checks for missing keys in a node.
      Parameters:
        `node` - the JsonNode to be checked
        `keys` - key names
      Returns:
        true if one or more keys are missing, false otherwise

    - #### objectNode

          protected com.fasterxml.jackson.databind.node.ObjectNode objectNode()

    - #### arrayNode

          protected com.fasterxml.jackson.databind.node.ArrayNode arrayNode()

    - #### longVal

          protected static long longVal(com.fasterxml.jackson.databind.JsonNode node,
                     String name)

Copyright © 2014. All Rights Reserved.