# `JSONCodec`

digraph inheritancee6d6bf04cd { bgcolor=transparent; rankdir=UD; ratio=compress; size="8.0, 12.0"; "Codec" [URL="taurus.core.util.codecs-Codec.html#taurus.core.util.codecs.Codec",color=dodgerblue1,fillcolor=white,fontcolor=black,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.5,shape=box,style=rounded,target="_top",tooltip="The base class for all codecs"]; "Logger" -> "Codec" [arrowsize=0.5,style="setlinewidth(0.5)"]; "JSONCodec" [URL="#taurus.core.util.codecs.JSONCodec",color=dodgerblue1,fillcolor=white,fontcolor=black,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.5,shape=box,style=rounded,target="_top",tooltip="A codec able to encode/decode to/from json format. It uses the"]; "Codec" -> "JSONCodec" [arrowsize=0.5,style="setlinewidth(0.5)"]; "Logger" [URL="taurus-Logger.html#taurus.Logger",color=dodgerblue1,fillcolor=white,fontcolor=black,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.5,shape=box,style=rounded,target="_top",tooltip="The taurus logger class. All taurus pertinent classes should inherit"]; }

class JSONCodec

  A codec able to encode/decode to/from json format. It uses the `json` module.

  Example:

      >>> from taurus.core.util.codecs import CodecFactory

      >>> cf = CodecFactory()
      >>> codec = cf.getCodec('json')
      >>>
      >>> # first encode something
      >>> data = { 'hello' : 'world', 'goodbye' : 1000 }
      >>> format, encoded_data = codec.encode(("", data))
      >>> print(encoded_data)
      '{"hello": "world", "goodbye": 1000}'
      >>>
      >>> # now decode it
      >>> format, decoded_data = codec.decode((format, encoded_data))
      >>> print(decoded_data)
      {'hello': 'world', 'goodbye': 1000}

  Import from `taurus.core.util.codecs` as:

      from taurus.core.util.codecs import JSONCodec

  decode(data, *args, **kwargs)

    decodes the given data from a json string.

    Parameters:

      data (sequence[str, obj]) – a sequence of two elements where the first item is the encoding format of the second item object

    Returns:

      a sequence of two elements where the first item is the encoding format of the second item object

    Return type:

      sequence[str, obj]

  encode(data, *args, **kwargs)

    encodes the given data to a json string. The given data must be a python object that json is able to convert.

    Parameters:

      data (sequence[str, obj]) – a sequence of two elements where the first item is the encoding format of the second item object

    Returns:

      a sequence of two elements where the first item is the encoding format of the second item object

    Return type:

      sequence[str, obj]