GithubHelp home page GithubHelp logo

juuxel / jankson Goto Github PK

View Code? Open in Web Editor NEW

This project forked from falkreon/jankson

0.0 1.0 0.0 1.16 MB

JSON5 / HJSON parser and preprocessor which preserves ordering and comments

License: MIT License

Java 100.00%

jankson's Introduction

JSON5 / HJSON parser and preprocessor that preserves ordering and comments

Official Discord: https://discord.gg/tV6FYXE8QH

The full list of JSON5 quirks, and several HJSON quirks are supported.

A full list of supported quirks is available on the wiki!

NOTE: This artifact isn't on MavenCentral yet! Check the 1.8 branch or the docs for code you can get from MavenCentral right now.

repositories {
	mavenCentral()
}

dependencies {
	"blue.endless:jankson:2.0.0"
}

Using

Jankson is, for the most part, a drop-in replacement for Gson or Hjson, but can also be used as a preprocessor to fix quirks and strip comments, re-baking it into standard JSON syntax for another parser to consume.

try {
	// configObject will represent the document root of the config file, and contains comments and formatting
	// that can be used to recreate the file with some minor formatting and indentation cleanup.
	ObjectElement configObject = Jankson.loadJsonObject(new File(configPath, "config.json"));
	
	
	String json5 = configObject.toString(); // toString for any JsonElement is its serialized form
	
	
	// Asking the writer to use STRICT json allows you to use Jankson as a preprocessor for other libraries
	StringWriter stringWriter = new StringWriter();
	JsonWriter jsonWriter = new JsonWriter(stringWriter, JsonWriterOptions.STRICT);
	configObject.write(jsonWriter);
	stringWriter.flush();
	String strictJson = stringWriter.toString(); //strictJson is your preprocessed data
	
} catch (IOException ex) {
	log.error("Couldn't read the config file", ex);
	return;
} catch (SyntaxError error) {
	log.error(error); // Stack traces printed or logged will be enhanced with line numbers
	return;
}

Displaying errors

In nearly any case where the processor can't accept the input, the SyntaxError subclass is capable of producing a String which describes both the line and character that the element started parsing at, and the line and character where the error was discovered.
When presenting a SyntaxError to the user, it's strongly recommended that the stack trace is omitted, and instead two lines are printed: the exception's getMessage(), followed by its getLineMessage().
This will give the user the most relevant information available about how to fix the problem. If multiple JSON files are being parsed, it may also be necessary to indicate the name and/or path to the file so that the problem can be located.

jankson's People

Contributors

falkreon avatar juuxel avatar halotroop2288 avatar unascribed avatar nikkyai avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.