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

swim.json.JsonParser Maven / Gradle / Ivy

// Copyright 2015-2019 SWIM.AI inc.
//
// 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.

package swim.json;

import java.math.BigInteger;
import swim.codec.Diagnostic;
import swim.codec.Input;
import swim.codec.Output;
import swim.codec.Parser;
import swim.codec.Unicode;
import swim.util.Builder;

/**
 * Factory for constructing JSON parsers and parse trees.
 */
public abstract class JsonParser {
  public abstract I item(V value);

  public abstract V value(I item);

  public abstract I field(V key, V value);

  public abstract Builder objectBuilder();

  public abstract Builder arrayBuilder();

  public abstract Output textOutput();

  public abstract V ident(V value);

  public abstract V num(int value);

  public abstract V num(long value);

  public abstract V num(float value);

  public abstract V num(double value);

  public abstract V num(BigInteger value);

  public abstract V num(String value);

  public abstract V uint32(int value);

  public abstract V uint64(long value);

  public abstract V bool(boolean value);

  public Parser parseValue(Input input) {
    return ValueParser.parse(input, this);
  }

  public Parser parseObject(Input input) {
    return ObjectParser.parse(input, this);
  }

  public Parser parseArray(Input input) {
    return ArrayParser.parse(input, this);
  }

  public Parser parseIdent(Input input) {
    return IdentParser.parse(input, this);
  }

  public Parser parseString(Input input) {
    return StringParser.parse(input, this);
  }

  public Parser parseNumber(Input input) {
    return NumberParser.parse(input, this);
  }

  public Parser valueParser() {
    return new ValueParser(this);
  }

  public Parser objectParser() {
    return new ObjectParser(this);
  }

  public Parser arrayParser() {
    return new ArrayParser(this);
  }

  public V parseValueString(String string) {
    Input input = Unicode.stringInput(string);
    while (input.isCont() && Json.isWhitespace(input.head())) {
      input = input.step();
    }
    Parser parser = parseValue(input);
    if (parser.isDone()) {
      while (input.isCont() && Json.isWhitespace(input.head())) {
        input = input.step();
      }
    }
    if (input.isCont() && !parser.isError()) {
      parser = Parser.error(Diagnostic.unexpected(input));
    } else if (input.isError()) {
      parser = Parser.error(input.trap());
    }
    return parser.bind();
  }

  public V parseObjectString(String string) {
    Input input = Unicode.stringInput(string);
    while (input.isCont() && Json.isWhitespace(input.head())) {
      input = input.step();
    }
    Parser parser = parseObject(input);
    if (parser.isDone()) {
      while (input.isCont() && Json.isWhitespace(input.head())) {
        input = input.step();
      }
    }
    if (input.isCont() && !parser.isError()) {
      parser = Parser.error(Diagnostic.unexpected(input));
    } else if (input.isError()) {
      parser = Parser.error(input.trap());
    }
    return parser.bind();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy