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

it.tidalwave.semantic.io.json.JsonPrettyPrinterStream Maven / Gradle / Ivy

There is a newer version: 1.0.16
Show newest version
/***********************************************************************************************************************
 *
 * blueBill Mobile - open source birdwatching
 * ==========================================
 *
 * Copyright (C) 2009, 2010 by Tidalwave s.a.s. (http://www.tidalwave.it)
 * http://bluebill.tidalwave.it/mobile/
 *
 ***********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 ***********************************************************************************************************************
 *
 * $Id: JsonPrettyPrinterStream.java,v c43881ee6e21 2010/07/11 12:03:30 fabrizio $
 *
 **********************************************************************************************************************/
package it.tidalwave.semantic.io.json;

import javax.annotation.Nonnull;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id: $
 *
 **********************************************************************************************************************/
public class JsonPrettyPrinterStream extends FilterOutputStream
  {
    private int indent = 0;
    private boolean insideArray = false;
    private boolean insideString = false;
    private boolean insideObjects = false;

    public JsonPrettyPrinterStream (final @Nonnull OutputStream os)
      {
        super(os);
      }

    @Override
    public void write (final int c)
      throws IOException
      {
        if (!insideString && (c == '"'))
          {
            insideString = true;
            super.write(c);
            return;
          }

        if (insideString)
          {
            super.write(c);

            if (c == '"')
              {
                insideString = false;
              }
            
            return;
          }

        if (c == '[')
          {
            insideArray = true;
            indent += 2;
            indent();
            super.write(c);
            indent += 2;
            indent();
            return;
          }
        if (c == ']')
          {
            insideArray = false;
            indent -= 2;
            indent();
            super.write(c);
            indent -= 2;
            return;
          }

        if (insideArray)
          {
            super.write(c);

            if ((c == ',') && !insideObjects)
              {
                indent();
              }

            if (c == '{')
              {
                insideObjects = true;
              }
            else if (c == '}')
              {
                insideObjects = false;
              }
          }
        else
          {
            if ((c == '{'))
              {
                indent += 2;
                indent();
                super.write(c);
                indent += 2;
                indent();
              }
            else if ((c == '}'))
              {
                indent -= 2;
                indent();
                super.write(c);
                indent -= 2;
                indent();
              }
            else
              {
                super.write(c);

                if (c == ',')
                  {
                    indent();
                  }
                else if (c == ':')
                  {
                    super.write(' ');
                  }
              }
          }
      }

    private void indent()
      throws IOException
      {
        super.write('\n');
        
        for (int i = 0; i < indent; i++)
          {
            super.write(' ');
          }
      }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy