Class JsonWriter


  • class JsonWriter
    extends java.lang.Object
    Outputs a SAMP object as JSON. Can do it formatted and reasonably compact.
    Since:
    25 Jul 2011
    Author:
    Mark Taylor
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private int indent_  
      private java.lang.String spc_  
    • Constructor Summary

      Constructors 
      Constructor Description
      JsonWriter()
      Constructor with default properties.
      JsonWriter​(int indent, boolean spacer)
      Custom constructor.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private java.lang.String getIndent​(int level)
      Returns prepended whitespace.
      private java.lang.String getIntroIndent​(int level, char chr, boolean isPositioned)
      Returns prepended whitespace containing an opener character.
      static void main​(java.lang.String[] args)  
      java.lang.String toJson​(java.lang.Object item)
      Converts a SAMP data item to JSON.
      private void toJson​(java.lang.StringBuffer sbuf, java.lang.Object item, int level, boolean isPositioned)
      Recursive method which does the work for conversion.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • indent_

        private final int indent_
      • spc_

        private final java.lang.String spc_
    • Constructor Detail

      • JsonWriter

        public JsonWriter()
        Constructor with default properties.
      • JsonWriter

        public JsonWriter​(int indent,
                          boolean spacer)
        Custom constructor.
        Parameters:
        indent - number of characters indent per level
        spacer - whether to put spaces inside brackets
    • Method Detail

      • toJson

        public java.lang.String toJson​(java.lang.Object item)
        Converts a SAMP data item to JSON.
        Parameters:
        item - SAMP-friendly object
        Returns:
        JSON representation
      • toJson

        private void toJson​(java.lang.StringBuffer sbuf,
                            java.lang.Object item,
                            int level,
                            boolean isPositioned)
        Recursive method which does the work for conversion. If possible, call this method with isPositioned=false.
        Parameters:
        sbuf - string buffer to append result to
        item - object to convert
        level - current indentation level
        isPositioned - true if output should be direct to sbuf, false if it needs a newline plus indentation first
      • getIntroIndent

        private java.lang.String getIntroIndent​(int level,
                                                char chr,
                                                boolean isPositioned)
        Returns prepended whitespace containing an opener character.
        Parameters:
        level - indentation level
        chr - opener character
        isPositioned - true if output should be direct to sbuf, false if it needs a newline plus indentation first
        Returns:
        string to prepend
      • getIndent

        private java.lang.String getIndent​(int level)
        Returns prepended whitespace.
        Parameters:
        level - indentation level
        Returns:
        string to prepend
      • main

        public static void main​(java.lang.String[] args)