<commit_before>package com.sigpwned.jsonification.generator;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import com.sigpwned.jsonification.JsonError;
import com.sigpwned.jsonification.exception.GenerateJsonException;

/**
 * Copyright 2015 <NAME>
 *     
 * 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.
 */
public class JsonGenerator implements AutoCloseable {
    private static class Scope {
        public static enum Type {
            ROOT, OBJECT, ARRAY;
        }
        
        public final Scope.Type type;
        public int count;
        
        public Scope(Scope.Type type) {
            this.type = type;
            this.count = 0;
        }
        
        public String toString() {
            return type.name();
        }
    }
    
    private final Writer writer;
    private final char[] cpbuf;
    private final List<Scope> scopes;
    
    public JsonGenerator(Writer writer) {
        this.writer = writer;
        this.cpbuf = new char[2];
        this.scopes = new ArrayList<>();
        this.scopes.add(new Scope(Scope.Type.ROOT));
    }
    
    public void openObject() throws IOException {
        openObject(null);
    }
    
    public void openObject(String name) throws IOException {
        Scope top=scope();
        if(top.type!=Scope.Type.ROOT && top.count!=0)
            writer.write(",");
        name(name);
        writer.write("{");
        scopes.add(new Scope(Scope.Type.OBJECT));
    }
    
    public void closeObject() throws IOException {
        Scope top=scopes.remove(scopes.size()-1);
        if(top.type != Scope.Type.OBJECT)
            throw new GenerateJsonException("cannot close object in "+top+" scope");
        writer.write("}");
        scope().count += 1;
    }
    
    public void openArray() throws IOException {
        openArray(null);
    }
    
    public void openArray(String name) throws IOException {
        Scope top=scope();
        if(top.type!=Scope.Type.ROOT && top.count!=0)
            writer.write(",");
        name(name);
        writer.write("[");
        scopes.add(new Scope(Scope.Type.ARRAY));
    }
    
    public void closeArray() throws IOException {
        Scope top=scopes.remove(scopes.size()-1);
        if(top.type != Scope.Type.ARRAY)
            throw new GenerateJsonException("cannot close object in "+top+" scope");
        writer.write("]");
        scope().count += 1;
    }
    
    public void scalar(long value) throws IOException {
        scalar(null, value);
    }
    
    public void scalar(String name, long value) throws IOException {
        Scope top=scope();
        if(top.type!=Scope.Type.ROOT && top.count!=0)
            writer.write(",");
        name(name);
        writer.write(Long.toString(value));
        top.count = top.count+1;
    }
    
    public void scalar(double value) throws IOException {
        scalar(null, value);
    }
    
    public void scalar(String name, double value) throws IOException {
        Scope top=scope();
        if(top.type!=Scope.Type.ROOT && top.count!=0)
            writer.write(",");
        name(name);
        writer.write(Double.toString(value));
        top.count = top.count+1;
    }
    
    public void scalar(String value) throws IOException {
        scalar(null, value);
    }
    
    public void scalar(String name, String value) throws IOException {
        Scope top=scope();
        if(top.type!=Scope.Type.ROOT && top.count!=0)
            writer.write(",");
        name(name);
        writer.write(string(value));
        top.count = top.count+1;
    }
    
    public void scalar(boolean value) throws IOException {
        scalar(null, value);
    }
    
    public void scalar(String name, boolean value) throws IOException {
        Scope top=scope();
        if(top.type!=Scope.Type.ROOT && top.count!=0)
            writer.write(",");
        name(name);
        writer.write(Boolean.toString(value));
        top.count = top.count+1;
    }
    
    public void nil() throws IOException {
        nil(null);
    }
    
    public void nil(String name) throws IOException {
        Scope top=scope();<commit_msg>added primitive Object methods to make managing null scalars easier<commit_after>package com.sigpwned.jsonification.generator;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import com.sigpwned.jsonification.JsonError;
import com.sigpwned.jsonification.exception.GenerateJsonException;

/**
 * Copyright 2015 <NAME>
 *     
 * 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.
 */
public class JsonGenerator implements AutoCloseable {
    private static class Scope {
        public static enum Type {
            ROOT, OBJECT, ARRAY;
        }
        
        public final Scope.Type type;
        public int count;
        
        public Scope(Scope.Type type) {
            this.type = type;
            this.count = 0;
        }
        
        public String toString() {
            return type.name();
        }
    }
    
    private final Writer writer;
    private final char[] cpbuf;
    private final List<Scope> scopes;
    
    public JsonGenerator(Writer writer) {
        this.writer = writer;
        this.cpbuf = new char[2];
        this.scopes = new ArrayList<>();
        this.scopes.add(new Scope(Scope.Type.ROOT));
    }
    
    public void openObject() throws IOException {
        openObject(null);
    }
    
    public void openObject(String name) throws IOException {
        Scope top=scope();
        if(top.type!=Scope.Type.ROOT && top.count!=0)
            writer.write(",");
        name(name);
        writer.write("{");
        scopes.add(new Scope(Scope.Type.OBJECT));
    }
    
    public void closeObject() throws IOException {
        Scope top=scopes.remove(scopes.size()-1);
        if(top.type != Scope.Type.OBJECT)
            throw new GenerateJsonException("cannot close object in "+top+" scope");
        writer.write("}");
        scope().count += 1;
    }
    
    public void openArray() throws IOException {
        openArray(null);
    }
    
    public void openArray(String name) throws IOException {
        Scope top=scope();
        if(top.type!=Scope.Type.ROOT && top.count!=0)
            writer.write(",");
        name(name);
        writer.write("[");
        scopes.add(new Scope(Scope.Type.ARRAY));
    }
    
    public void closeArray() throws IOException {
        Scope top=scopes.remove(scopes.size()-1);
        if(top.type != Scope.Type.ARRAY)
            throw new GenerateJsonException("cannot close object in "+top+" scope");
        writer.write("]");
        scope().count += 1;
    }
    
    public void scalar(Long value) throws IOException {
        scalar(null, value);
    }
    
    public void scalar(String name, Long value) throws IOException {
        if(value != null)
            scalar(name, value.longValue());
        else
            nil(name);
    }
    
    public void scalar(long value) throws IOException {
        scalar(null, value);
    }
    
    public void scalar(String name, long value) throws IOException {
        Scope top=scope();
        if(top.type!=Scope.Type.ROOT && top.count!=0)
            writer.write(",");
        name(name);
        writer.write(Long.toString(value));
        top.count = top.count+1;
    }
    
    public void scalar(Double value) throws IOException {
        scalar(null, value);
    }
    
    public void scalar(String name, Double value) throws IOException {
        if(value != null)
            scalar(name, value.doubleValue());
        else
            nil(name);
    }
    
    public void scalar(double value) throws IOException {
        scalar(null, value);
    }
    
    public void scalar(String name, double value) throws IOException {
        Scope top=scope();
        if(top.type!=Scope.Type.ROOT && top.count!=0)
            writer.write(",");
        name(name);
        writer.write(Double.toString(value));
        top.count = top.count+1;
    }
    
    public void scalar(String value) throws IOException {
        scalar(null, value);
    }
    
    public void scalar(String name, String value) throws IOException {
        Scope top=scope();
        if(top.type!=Scope.Type.ROOT && top.count!=0)
            writer.write(",");
        name(name);
        writer.write(string(value));
        top.count = top.count+1;
    }
    
    public void scalar(Boolean value) throws IOException {
        scalar(null, value);
    }
    
    public void scalar(String name, Boolean value) throws IOException {
        if(value != null)
            scalar(name, value.booleanValue());
        else
            nil(name);
    }
    
    public void scalar(boolean value) throws IOException {
        scalar(null, value);
    }
    
    public void scalar(String name, boolean value) throws IOException {
        Scope top=scope();
        if(top.type!=Scope.Type.ROOT && top.count!=0)
            writer.write(",");
        name(name);
        writer.write(Boolean.toString(value));
        top.count = top.count+1;
    }
    
    public void nil() throws IOException {
        nil(null);
    }
    
    public void nil(String name) throws IOException {
        Scope top=scope();
{'fraction_non_alphanumeric': 0.11332684824902724, 'fraction_numerical': 0.004669260700389105, 'mean_word_length': 2.208801498127341, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7250816', 'n_tokens_mistral': 2838, 'n_tokens_neox': 2809, 'n_words': 776}