All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.eclipsesource.json.JsonWriter Maven / Gradle / Ivy

/*******************************************************************************
 * Copyright (c) 2013, 2014 EclipseSource.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 ******************************************************************************/
package com.eclipsesource.json;

import java.io.IOException;
import java.io.Writer;


class JsonWriter {

  private static final int CONTROL_CHARACTERS_START = 0x0000;
  private static final int CONTROL_CHARACTERS_END = 0x001f;

  private static final char[] QUOT_CHARS = { '\\', '"' };
  private static final char[] BS_CHARS = { '\\', '\\' };
  private static final char[] LF_CHARS = { '\\', 'n' };
  private static final char[] CR_CHARS = { '\\', 'r' };
  private static final char[] TAB_CHARS = { '\\', 't' };
  // In JavaScript, U+2028 and U+2029 characters count as line endings and must be encoded.
  // http://stackoverflow.com/questions/2965293/javascript-parse-error-on-u2028-unicode-character
  private static final char[] UNICODE_2028_CHARS = { '\\', 'u',  '2', '0', '2', '8' };
  private static final char[] UNICODE_2029_CHARS = { '\\', 'u',  '2', '0', '2', '9' };
  private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                                             'a', 'b', 'c', 'd', 'e', 'f' };

  protected final Writer writer;

  JsonWriter( Writer writer ) {
    this.writer = writer;
  }

  void write( String string ) throws IOException {
    writer.write( string );
  }

  void writeString( String string ) throws IOException {
    writer.write( '"' );
    int length = string.length();
    int start = 0;
    char[] chars = new char[ length ];
    string.getChars( 0, length, chars, 0 );
    for( int index = 0; index < length; index++ ) {
      char[] replacement = getReplacementChars( chars[index] );
      if( replacement != null ) {
        writer.write( chars, start, index - start );
        writer.write( replacement );
        start = index+1;
      }
    }
    writer.write( chars, start, length - start );
    writer.write( '"' );
  }

  private static char[] getReplacementChars( char ch ) {
    char[] replacement = null;
    if( ch == '"' ) {
      replacement = QUOT_CHARS;
    } else if( ch == '\\' ) {
      replacement = BS_CHARS;
    } else if( ch == '\n' ) {
      replacement = LF_CHARS;
    } else if( ch == '\r' ) {
      replacement = CR_CHARS;
    } else if( ch == '\t' ) {
      replacement = TAB_CHARS;
    } else if( ch == '\u2028' ) {
      replacement = UNICODE_2028_CHARS;
    } else if( ch == '\u2029' ) {
      replacement = UNICODE_2029_CHARS;
    } else if( ch >= CONTROL_CHARACTERS_START && ch <= CONTROL_CHARACTERS_END ) {
      replacement = new char[] { '\\', 'u',  '0', '0', '0', '0' };
      replacement[4] = HEX_DIGITS[ ch >> 4 & 0x000f ];
      replacement[5] = HEX_DIGITS[ ch & 0x000f ];
    }
    return replacement;
  }

  protected void writeObject( JsonObject object ) throws IOException {
    writeBeginObject();
    boolean first = true;
    for( JsonObject.Member member : object ) {
      if( !first ) {
        writeObjectValueSeparator();
      }
      writeString( member.getName() );
      writeNameValueSeparator();
      member.getValue().write( this );
      first = false;
    }
    writeEndObject();
  }

  protected void writeBeginObject() throws IOException {
    writer.write( '{' );
  }

  protected void writeEndObject() throws IOException {
    writer.write( '}' );
  }

  protected void writeNameValueSeparator() throws IOException {
    writer.write( ':' );
  }

  protected void writeObjectValueSeparator() throws IOException {
    writer.write( ',' );
  }

  protected void writeArray( JsonArray array ) throws IOException {
    writeBeginArray();
    boolean first = true;
    for( JsonValue value : array ) {
      if( !first ) {
        writeArrayValueSeparator();
      }
      value.write( this );
      first = false;
    }
    writeEndArray();
  }

  protected void writeBeginArray() throws IOException {
    writer.write( '[' );
  }

  protected void writeEndArray() throws IOException {
    writer.write( ']' );
  }

  protected void writeArrayValueSeparator() throws IOException {
    writer.write( ',' );
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy