Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2007 Mozilla Foundation
   *
   * 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 nu.validator.json;
 
 import java.io.Writer;
 import java.util.List;
 
 
 public class Serializer implements JsonHandler {
 
     private enum State {
         INITIAL, DOCUMENT, ARRAY, OBJECT, VALUE, STRING
     }
 
     private final List<Statestack = new ArrayList<State>();
 
     private boolean hadCallback = false;
 
     private boolean first = false;
 
     private final Writer writer;
 
     private static Writer newOutputStreamWriter(OutputStream out) {
         CharsetEncoder enc = Charset.forName("UTF-8").newEncoder();
         return new OutputStreamWriter(outenc);
     }
     
     public Serializer(OutputStream out) {
         this. = newOutputStreamWriter(out);
         push(.);
     }
 
     private void push(State state) {
         .add(state);
     }
 
     private void pop() {
         .remove(.size() - 1);
     }
 
     private State peek() {
         int size = .size();
         if (size == 0) {
             return null;
         } else {
             return .get(size - 1);
         }
     }
 
     public void bool(boolean boolthrows SAXException {
         try {
             State state = peek();
             switch (state) {
                 case :
                     if (!) {
                         .write(',');
                     }
                     // fall thru
                 case :
                 case :
                     .write(Boolean.toString(bool));
                     if (state == .) {
                         pop();
                     }
                      = false;
                     break;
                 default:
                     throw new SAXException("Illegal state for callback.");
             }
        } catch (IOException e) {
            throw new SAXException(e.getMessage(), e);
        }
    }
    private void charactersImpl(char[] chint startint length)
            throws IOException {
        int s = start;
        int end = start + length;
        for (int i = starti < endi++) {
            char c = ch[i];
            if (c <= '\u001F' || c == '\"' || c == '\\') {
                if (s < i) {
                    .write(chsi - s);
                }
                s = i + 1;
                .write('\\');
                switch (c) {
                    case '\"':
                        .write('\"');
                        break;
                    case '\\':
                        .write('\\');
                        break;
                    case '\u0008':
                        .write('b');
                        break;
                    case '\u000C':
                        .write('f');
                        break;
                    case '\n':
                        .write('n');
                        break;
                    case '\r':
                        .write('r');
                        break;
                    case '\t':
                        .write('t');
                        break;
                    default:
                        String hex = Integer.toHexString(c);
                        if (hex.length() == 1) {
                            .write("u000");
                            .write(hex);
                        } else {
                            .write("u00");
                            .write(hex);                            
                        }
                        break;
                }
            }
        }
        if (s < end) {
            .write(chsend - s);
        }
    }
    public void characters(char[] chint startint length)
            throws SAXException {
        try {
            State state = peek();
            switch (state) {
                case :
                    charactersImpl(chstartlength);
                    break;
                default:
                    throw new SAXException("Illegal state for callback.");
            }
        } catch (IOException e) {
            throw new SAXException(e.getMessage(), e);
        }
    }
    public void endArray() throws SAXException {
        try {
            State state = peek();
            switch (state) {
                case :
                    .write(']');
                    pop();
                     = false;
                    if (peek() == .) {
                        pop();
                    }
                    break;
                default:
                    throw new SAXException("Illegal state for callback.");
            }
        } catch (IOException e) {
            throw new SAXException(e.getMessage(), e);
        }
    }
    public void endDocument() throws SAXException {
        try {
            State state = peek();
            switch (state) {
                case :
                    if () {
                        .write(')');
                    }
                    .write('\n');
                    .flush();
                    .close();
                    pop();
                    break;
                default:
                    throw new SAXException("Illegal state for callback.");
            }
        } catch (IOException e) {
            throw new SAXException(e.getMessage(), e);
        }
    }
    public void endObject() throws SAXException {
        try {
            State state = peek();
            switch (state) {
                case :
                    .write('}');
                    pop();
                     = false;
                    if (peek() == .) {
                        pop();
                    }
                    break;
                default:
                    throw new SAXException("Illegal state for callback.");
            }
        } catch (IOException e) {
            throw new SAXException(e.getMessage(), e);
        }
    }
    public void endString() throws SAXException {
        try {
            State state = peek();
            switch (state) {
                case :
                    .write('\"');
                    pop();
                     = false;
                    if (peek() == .) {
                        pop();
                    }
                    break;
                default:
                    throw new SAXException("Illegal state for callback.");
            }
        } catch (IOException e) {
            throw new SAXException(e.getMessage(), e);
        }
    }
    public void key(String keythrows SAXException {
        try {
            State state = peek();
            switch (state) {
                case :
                    if (!) {
                        .write(',');
                    }
                    .write('\"');
                    charactersImpl(key.toCharArray(), 0, key.length());
                    .write('\"');
                    .write(':');
                    push(.);
                    break;
                default:
                    throw new SAXException("Illegal state for callback.");
            }
        } catch (IOException e) {
            throw new SAXException(e.getMessage(), e);
        }
    }
    public void number(int numberthrows SAXException {
        try {
            State state = peek();
            switch (state) {
                case :
                    if (!) {
                        .write(',');
                    }
                    // fall thru
                case :
                case :
                    .write(Integer.toString(number));
                    if (state == .) {
                        pop();
                    }
                     = false;
                    break;
                default:
                    throw new SAXException("Illegal state for callback.");
            }
        } catch (IOException e) {
            throw new SAXException(e.getMessage(), e);
        }
    }
    public void number(long numberthrows SAXException {
        try {
            State state = peek();
            switch (state) {
                case :
                    if (!) {
                        .write(',');
                    }
                    // fall thru
                case :
                case :
                    .write(Long.toString(number));
                    if (state == .) {
                        pop();
                    }
                     = false;
                    break;
                default:
                    throw new SAXException("Illegal state for callback.");
            }
        } catch (IOException e) {
            throw new SAXException(e.getMessage(), e);
        }
    }
    public void number(float numberthrows SAXException {
        try {
            State state = peek();
            switch (state) {
                case :
                    if (!) {
                        .write(',');
                    }
                    // fall thru
                case :
                case :
                    .write(Float.toString(number));
                    if (state == .) {
                        pop();
                    }
                     = false;
                    break;
                default:
                    throw new SAXException("Illegal state for callback.");
            }
        } catch (IOException e) {
            throw new SAXException(e.getMessage(), e);
        }
    }
    public void number(double numberthrows SAXException {
        try {
            State state = peek();
            switch (state) {
                case :
                    if (!) {
                        .write(',');
                    }
                    // fall thru
                case :
                case :
                    .write(Double.toString(number));
                    if (state == .) {
                        pop();
                    }
                     = false;
                    break;
                default:
                    throw new SAXException("Illegal state for callback.");
            }
        } catch (IOException e) {
            throw new SAXException(e.getMessage(), e);
        }
    }
    public void startArray() throws SAXException {
        try {
            State state = peek();
            switch (state) {
                case :
                    if (!) {
                        .write(',');
                    }
                    // fall thru
                case :
                case :
                    .write('[');
                    push(.);
                     = true;
                    break;
                default:
                    throw new SAXException("Illegal state for callback.");
            }
        } catch (IOException e) {
            throw new SAXException(e.getMessage(), e);
        }
    }
    public void startDocument(String callbackthrows SAXException {
        try {
            State state = peek();
            switch (state) {
                case :
                    if (callback == null) {
                         = false;
                    } else {
                         = true;
                        .write(callback);
                        .write('(');
                    }
                    push(.);
                     = true;
                    break;
                default:
                    throw new SAXException("Illegal state for callback.");
            }
        } catch (IOException e) {
            throw new SAXException(e.getMessage(), e);
        }
    }
    public void startObject() throws SAXException {
        try {
            State state = peek();
            switch (state) {
                case :
                    if (!) {
                        .write(',');
                    }
                    // fall thru
                case :
                case :
                    .write('{');
                    push(.);
                     = true;
                    break;
                default:
                    throw new SAXException("Illegal state for callback.");
            }
        } catch (IOException e) {
            throw new SAXException(e.getMessage(), e);
        }
    }
    public void startString() throws SAXException {
        try {
            State state = peek();
            switch (state) {
                case :
                    if (!) {
                        .write(',');
                    }
                    // fall thru
                case :
                case :
                    .write('\"');
                    push(.);
                    break;
                default:
                    throw new SAXException("Illegal state for callback.");
            }
        } catch (IOException e) {
            throw new SAXException(e.getMessage(), e);
        }
    }
    public void string(String stringthrows SAXException {
        try {
            State state = peek();
            switch (state) {
                case :
                    if (!) {
                        .write(',');
                    }
                    // fall thru
                case :
                case :
                    if (string == null) {
                        .write("null");
                    } else {
                        .write('\"');
                        charactersImpl(string.toCharArray(), 0, string.length());
                        .write('\"');
                    }
                    if (state == .) {
                        pop();
                    }
                     = false;
                    break;
                default:
                    throw new SAXException("Illegal state for callback.");
            }
        } catch (IOException e) {
            throw new SAXException(e.getMessage(), e);
        }
    }
New to GrepCode? Check out our FAQ X