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

gw.lang.reflect.json.DefaultParser Maven / Gradle / Ivy

There is a newer version: 1.18.2
Show newest version
package gw.lang.reflect.json;

import javax.script.Bindings;
import javax.script.ScriptException;
import java.io.StringReader;
import java.util.List;

public class DefaultParser implements IJsonParser {
  private static final DefaultParser INSTANCE = new DefaultParser();

  public static IJsonParser instance()
  {
    return INSTANCE;
  }

  @Override
  public Bindings parseJson( String jsonText ) throws ScriptException
  {
    return parseJson( jsonText, false );
  }
  public Bindings parseJson( String jsonText, boolean big ) throws ScriptException
  {
    SimpleParserImpl parser = new SimpleParserImpl( new Tokenizer(new StringReader(jsonText)), big );
    Object result = parser.parse();
    List errors = parser.getErrors();
    if(errors.size() != 0) {
      StringBuilder sb = new StringBuilder("Found errors:\n");
      for(String err : errors) {
        sb.append(err).append("\n");
      }
      throw new ScriptException(sb.toString());
    }
    if(result instanceof Bindings) {
      return (Bindings)result;
    }
    return NashornJsonParser.wrapValueInBindings( result );
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy