Hjson for Java

Related tags

Spring Boot java hjson
Overview

hjson-java

Build Status Maven Central Javadoc

Hjson, the Human JSON. A configuration file format for humans. Relaxed syntax, fewer mistakes, more comments.

Hjson Intro

{
  # specify rate in requests/second (because comments are helpful!)
  rate: 1000

  // prefer c-style comments?
  /* feeling old fashioned? */

  # did you notice that rate doesn't need quotes?
  hey: look ma, no quotes for strings either!

  # best of all
  notice: []
  anything: ?

  # yes, commas are optional!
}

The Java implementation of Hjson is based on minimal-json. For other platforms see hjson.github.io.

CLI

You can install the Hjson command line tool by downloading and unpacking the latest hjson.zip.

  • run hjson -h for help
  • hjson file.json will convert to Hjson.
  • hjson -j file.hjson will convert to JSON.

Install from Maven Central

Gradle

Add a dependency to your build.gradle:

dependencies {
  compile 'org.hjson:hjson:3.0.0'
}

Maven

Add a dependency to your pom.xml:

<dependency>
  <groupId>org.hjson</groupId>
  <artifactId>hjson</artifactId>
  <version>3.0.0</version>
</dependency>

Ivy

Add a dependency to your ivy.xml:

<dependencies>
  <dependency org="org.hjson" name="hjson" rev="3.0.0"/>
</dependencies>

Usage

You can either

  • use this libary directly
  • or just convert Hjson to JSON and use it with your favorite JSON library.

Convert

// convert Hjson to JSON
String jsonString = JsonValue.readHjson(readerOrHjsonString).toString();

// convert JSON to Hjson
String hjsonString = JsonValue.readHjson(readerOrJSONString).toString(Stringify.HJSON);

Read

JsonObject jsonObject = JsonValue.readHjson(string).asObject();
JsonArray jsonArray = JsonValue.readHjson(reader).asArray();

JsonValue.readHjson() will accept both Hjson and JSON. You can use JsonValue.readJSON() to accept JSON input only.

Object sample

String name = jsonObject.get("name").asString();
int age = jsonObject.get("age").asInt(); // asLong(), asFloat(), asDouble(), ...

// or iterate over the members
for (Member member : jsonObject) {
  String name = member.getName();
  JsonValue value = member.getValue();
  // ...
}

Array sample

String name = jsonArray.get(0).asString();
int age = jsonArray.get(1).asInt(); // asLong(), asFloat(), asDouble(), ...

// or iterate over the values
for(JsonValue value : jsonArray) {
  // ...
}

Nested sample

// Example: { "friends": [ { "name": "John", "age": 23 }, ... ], ... }
JsonArray friends = jsonObject.get("friends").asArray();
String name = friends.get(0).asObject().get("name").asString();
int age = friends.get(0).asObject().get("age").asInt();

Create

JsonObject jsonObject = new JsonObject().add("name", "John").add("age", 23);
// -> { "name": "John", "age", 23 }

JsonArray jsonArray = new JsonArray().add("John").add(23);
// -> [ "John", 23 ]

Modify

jsonObject.set("age", 24);
jsonArray.set(1, 24); // access element by index

jsonObject.remove("age");
jsonArray.remove(1);

Write

Writing is not buffered (to avoid buffering twice), so you should use a BufferedWriter.

jsonObject.writeTo(writer);
jsonObject.writeTo(writer, Stringify.HJSON);

toString()

jsonObject.toString(Stringify.HJSON); // Hjson output
jsonObject.toString(Stringify.FORMATTED); // formatted JSON output
jsonObject.toString(Stringify.PLAIN); // plain JSON output, default
jsonObject.toString(); // plain

API

Documentation

History

see history.md

Comments
  • whitespace between unquotedName and colon(:) causes ParseException

    whitespace between unquotedName and colon(:) causes ParseException

    The bug can be reproduced by "{x :1}" as following: String js = "{x :1}"; //{123, 120, 32, 58, 49, 125} org.hjson.JsonValue jv = org.hjson.JsonValue.readHjson(js);

    ParseException: Found ' ' where a key name was expected (check your syntax or use quotes if the key name includes {}[],: or whitespace) at 1:2

    opened by saijone 3
  • parsing failure with space after unquoted key

    parsing failure with space after unquoted key

    This works:

    x: {},

    The following does not work (space after x):

    x : {},

    throws "Found ' ' where a key name was expected". net.sf.json.JSONObject does parse this correctly however!

    opened by heywiki 3
  • hjson requires closing curly bracket in its own line

    hjson requires closing curly bracket in its own line

    This works fine with json:

    {foo:'bar'}
    

    It throws the following in hjson (2.1.1)

    org.hjson.ParseException: End of input while parsing an object (did you forget a closing '}'?) at 3:-1
    	at org.hjson.HjsonParser.error(HjsonParser.java:482)
    

    Only after you set the closing curly bracket in its own line, it works. My basic assumption is that hjson should be more forgiving than json. If hjson requires the closing bracket it its own line, than at least the error message is misleading.

    opened by heywiki 1
  • anonymous objects as array values

    anonymous objects as array values

    Parsing fails with anonymous object values within an array. Tested with 2.1.1:

    {
        pages:[
            { name:'X' }
        ]
    }
    

    Exception is

    org.hjson.ParseException: Found ']' where a key name was expected (check your syntax or use quotes if the key name includes {}[],: or whitespace) at 5:4
    	at org.hjson.HjsonParser.error(HjsonParser.java:482)
    
    opened by heywiki 1
  • ParseException raised on valid Hjson.

    ParseException raised on valid Hjson.

    The small hjson below is turned into Json on https://hjson.org/try.html:

    [
       {
          foo : "
          "
       }
    ]
    

    When this same small hjson is given to hjson-java, the following exception is raised:

    Exception in thread "main" org.hjson.ParseException: Expected valid string character at 3:8
      at org.hjson.HjsonParser.error(HjsonParser.java:482)
      at org.hjson.HjsonParser.expected(HjsonParser.java:476)
      at org.hjson.HjsonParser.readStringInternal(HjsonParser.java:280)
      at org.hjson.HjsonParser.readString(HjsonParser.java:272)
      at org.hjson.HjsonParser.readValue(HjsonParser.java:106)
      at org.hjson.HjsonParser.readObject(HjsonParser.java:189)
      at org.hjson.HjsonParser.readValue(HjsonParser.java:108)
      at org.hjson.HjsonParser.readArray(HjsonParser.java:163)
      at org.hjson.HjsonParser.readValue(HjsonParser.java:107)
      at org.hjson.HjsonParser.parse(HjsonParser.java:81)
      at org.hjson.JsonValue.readHjson(JsonValue.java:130)
      at my.HjsonPOC.main(HjsonPOC.java:16)
    

    Here is a snippet to reproduce the problem:

    String test = "[\r\n" + //
        " {\r\n" + //
        " foo : \"\r\n" + //
        " \"\r\n" + //
        " }\r\n" + //
        "]";
    
    String jsonString = JsonValue.readHjson(test).toString(); //  ParseException raised !
    // Expected Json string:
    //[
    //  {
    //    "foo": "\r\n      "
    //  }
    //]
    
    opened by Stephan972 1
  • Tabs in multiline string

    Tabs in multiline string

    When the string contains tabs (\t), it's not exported as a multiline string, but as JSON-string. That significantly impacts readability.

    By HJSON syntax, there's to limit on tabs in the multiline string. Is it possible to export string with tabs as a multiline string?

    thanks

    opened by hundera 1
  • Option to disallow duplicate keys in input

    Option to disallow duplicate keys in input

    It'd be useful to be able to configure the parser to fail on duplicate keys in input in order to catch user errors in e.g. configuration files.

    I.e. fail on:

    some_config: {
      param1: foo
      param2: bar
      param2: baz  # Really intended to set param3 here but made a copypasta mistake
    }
    

    What do you think?

    opened by danielnorberg 1
  • Fixes issues with the Travis-CI build

    Fixes issues with the Travis-CI build

    Fix for #21

    Updates travis.yml

    • Removes Oracle JDK 7 and 8
    • Add Open JDK 8 and 11

    Updates build.gradle

    • Updates source and target compatibility to 1.8 (was 1.7)

    Improves .gitignore adds Eclipse, adds some comment, where the excluded files may come from.

    opened by verhagen 0
  • Reading in an array of strings, which is on one line, isn't possible

    Reading in an array of strings, which is on one line, isn't possible

    :white_check_mark: Array of integers on a single line works

    favNumbers: [ 1, 2, 3, 6, 42 ]
    

    :x: Array of plain Strings on a single line does not works

    tags: [ java, json, hjson ]
    

    :white_check_mark: Array of single quoted Strings on a single line does works

    tags: [ 'java', 'json', 'hjson' ]
    

    :white_check_mark: Array of double quoted Strings on a single line does works

    tags: [ "java", "json", "hjson" ]
    
    opened by verhagen 0
  • Build on Travis-CI causes issues

    Build on Travis-CI causes issues

    opened by verhagen 0
  • WritingBuffer is confusing

    WritingBuffer is confusing

    Can someone please explain this:

    "A lightweight writing buffer to reduce the amount of write operations to be performed on the underlying writer. This implementation is not thread-safe. It deliberately deviates from the contract of the Writer. In particular, it does not flush or close the wrapped writer nor does it ensure that the wrapped writer is open."

    Also, why is a BufferedWriter not being used?

    It's very concerning to see a writer have an empty close() method as this can lead to some very serious memory leaks. This implementation violates a lot of basic things in Java. I would like to help improve this but I don't currently know how since none of the code is documented and the code is not very readable.

    opened by jlgager24 0
  • JsonValue.valueOf( float / double ) should check NaN and ±Infinite values

    JsonValue.valueOf( float / double ) should check NaN and ±Infinite values

    Otherwise
    JsonValue.valueOf( Math.log(-1)).toString();
    will throw up a NumberFormatException like a drunken sailor, because inside JsonNumber.toString() the called function BigDecimal.valueOf() doesn't really like non-finite values.

    opened by biziclop 1
  • Would like to get unwrapped value from JsonValue

    Would like to get unwrapped value from JsonValue

    I notice a lot's of class have been extended from JsonValue, such as JsonString, JsonNumber.

    I am wondering why could not support getUnwrapped() value from the JsonValue.

    which, returns java.lang.Object. Such as Long, Integer, Double and String.

    That will be very helpful, since java.util.Properties need a Object value.

    opened by jeancky 0
  • Comment storage and formatting options

    Comment storage and formatting options

    I was working on another project using Hjson-java and realized it was missing a few things I needed. Namely, the ability to retain comments (my project uses them very liberally), as well as a few formatting options that I felt would make certain kinds of data a little bit more concise. So, I did just that. Here are some of the pros and cons that would be associated with merging this branch:

    New Features

    • Comments will be retained when parsed, and can also be added easily in-code.
    • There are three supported comment positions:
      • BOL: Beginning-of-line or before-line comments. These are placed on the line above any key or value. In the context of a root JsonObject, these will serve as the file's header.
      • EOL: End-of-line comments. These are placed after any value on the same line. In the context of a root JsonObject, these will serve as the file's footer and thus will be placed on a separate line.
      • INTERIOR: Essentially any other non-inline comment that isn't directly associated with a value. These might be used in an array or object with no fields or values.
    • Additionally, there are three supported comment styles:
      • HASH: Any text following a hash (#) symbol until the end of the current line. These do support multiple lines.
      • LINE: A C style line comment, indicated by two forward slashes (//). Like hash comments, these last until the end of the current line, but can technically be repeated on subsequent lines, with no issues.
      • BLOCK: These will encapsulate any text following a (/*) and preceding a (*/) and naturally can span multiple lines. Although users are free to include additional asterisks (*) on subsequent lines, these will not be placed when formatted by hjson-java.
    • Comments are stored inside of their respective JsonValue objects and ultimately must be added as such. Users can add comments in-code by calling JsonValue#setComment or any of its variants, as well as JsonObject#set, JsonObject#add, JsonObject#setComment or any of their variants, as well as the equivalent methods in JsonArray.
    • More information is saved about how configurations are formatted when parsing Hjson files, including:
      • Whether an array or object is "compact," i.e. whether the entire container is placed on a single line.
      • Whether multiple values in an array or object are placed per-line. If this number is inconsistent, the mean value will be taken and used, instead.
    • Users can apply a variety of new formatting options when using HjsonOptions and HjsonWriter.
      • indent: Indicates the number of spaces to be placed on each new line per-level.
      • commentIndent: Indicates the number of spaces to be placed before each comment that takes up its own line or lines.
      • nlBraces: Indicates whether braces and brackets should be placed on subsequent lines vs. the same line, as in the K&R or Java syntax.
      • allowCompact: Indicates whether values in an object or array should be allowed to remain compact (see above) when serialized.
      • allowMultiVal: Indicates whether multiple values in an object or array are allowed to be placed on a single line.
    • Values in a JsonObject can be sorted alphabetically by key by calling JsonObject#sort.
    • Setters inside of HjsonObjects now return this to enable chaining.
    • Removed unused imports.

    Potential Cons

    • JsonObject#sort relies on List#sort, which requires a lambda expression. I did not see these used anywhere else in the code, so I suspect that this may increase the minimum Java requirements to version 8. This can of course be removed, however.
    • Comments and other new settings inside of JsonObject and JsonArray are not technically made immutable when JsonObject#unmodifiableObject or equivalent function is called. I'm not sure if this is a memory safety issue or more of just a measure intended to prevent the actual data contained by the objects from being changed. If this is an issue, please let me know how you would solve it, so I can make changes.
    • I was not able to figure out why the final character inside of footers was not getting written by HjsonWriter. My workaround was to include an additional space at the end of the string when calling HjsonWriter#reset. I'm not very happy with this solution.
    • I did not get HjsonWriter#readBetweenVals (replacement for skipWhiteSpace()) to ignore the final new line character at the end of some comments. As a result, I wound up calling String#trim on the value that gets returned. This is technically not ideal and, as such, I'm not very happy with that either.
    • Potentially others. You may want to review.

    See it in Action

    You can view this test input file to see almost everything I listed in-action. You can also view this output file which was generated by parsing a JsonObject from a file, adding a few new keys and comments, and finally serializing the new object to the disk. And of course, if you do test this and discover any issues or notice anything right away that could be improved, please let me know and I may be able to work on it sometime soon.

    -PersonTheCat

    Edit: I see that the Travis CI build has failed due to an issue with using a lambda expression for JsonObject#sort. I'll wait to hear from someone else about whether this needs to be changed. Again, it could of course just be removed. See above.

    opened by PersonTheCat 7
Releases(v2.1.0)
Owner
Hjson
A user interface for JSON.
Hjson
Java-Programs---For-Practice is one of the Java Programming Practice Series By Shaikh Minhaj ( minhaj-313 ). This Series will help you to level up your Programming Skills. This Java Programs are very much helpful for Beginners.

Java-Programs---For-Practice is one of the Java Programming Practice Series By Shaikh Minhaj ( minhaj-313 ). This Series will help you to level up your Programming Skills. This Java Programs are very much helpful for Beginners. If You Have any doubt or query you can ask me here or you can also ask me on My LinkedIn Profile

Shaikh Minhaj 3 Nov 8, 2022
(Java & React) Yazılım Geliştirici Yetiştirme Kampı Java kısmına ait yazılan kaynak kodlar ve ödev çalışmalarım.

JavaCamp Kamp sürecinde yazılan kaynak kodlar ve ödev çalışmalarım. Day 1 1)Intro Day 2 2)oopIntro 2.1)oopIntro ~ Homework Day 3 3)oopIntro2 3.1)inher

Melih Çelik 17 Jun 26, 2022
☁ Tencent Cloud IM Server SDK in Java | 腾讯云 IM 服务端 SDK Java 版

Tencent Cloud IM Server SDK in Java The Tencent Cloud IM Server SDK for Java enables Java developers to easily work with Tencent Cloud IM. Requirement

Doocs 64 Dec 23, 2022
Trust-java - Test Results Verification library for Java

TRUST - Test Results Verification library for Java The TRUST's primary goal is to provide the simple way of different test results verification. Gener

Serhii Shymkiv 2 Nov 19, 2017
Changelog-java-version - Eine Auflistung der Änderungen in Java aus Sicht eines Entwicklers.

Changelog Java Versions Eine Auflistung der Änderungen in Java aus Sicht eines Entwicklers. Inhaltsverzeichnis Zugehörige Präsentation Homepage des Au

Frank W. Rahn 1 Jan 4, 2022
Bank Statement Analyzer Application that currently runs in terminal with the commands: javac Application.java java Application [file-name].csv GUI coming soon...

Bank Statement Analyzer Application that currently runs in terminal with the commands: javac Application.java java Application [file-name].csv GUI coming soon...

Hayden Hanson 0 May 21, 2022
Library for converting from one Java class to a dissimilar Java class with similar names based on the Bean convention

Beanmapper Beanmapper is a Java library for mapping dissimilar Java classes with similar names. The use cases for Beanmapper are the following: mappin

null 26 Nov 15, 2022
Free and 100% open source Progressive Java Runtime for modern Java™ deployments supported by a leading OpenJDK contributor

BellSoft Liberica JDK is a build of OpenJDK that is tested and verified to be compliant with the Java SE specification using OpenJDK Technology Compat

null 195 Dec 22, 2022
ActiveJ is an alternative Java platform built from the ground up. ActiveJ redefines web, high load, and cloud programming in Java, featuring ultimate performance and scalability!

Introduction ActiveJ is a full-featured modern Java platform, created from the ground up as an alternative to Spring/Micronauts/Netty/Jetty. It is des

ActiveJ LLC 579 Jan 7, 2023
Short Java programs for practice (OCP) Oracle Certified Professional Java SE 11

OCP-study Short Java programs to practice for (OCP) Oracle Certified Professional Java SE 11 Exam Google document with notes: https://docs.google.com/

Sabina Matjašič 1 May 24, 2022
Professional Java Developer Career Starter: Java Foundations Course Exercise Solutions

java-foundations-solutions Professional Java Developer Career Starter: Java Foundations Course Exercise Solutions The solutions are generally to be fo

Neutrino Systems Inc 41 Dec 28, 2022
Team 5468's 2022 FRC robot code. This code is written in Java and is based off of WPILib's Java control system and utilizes a command based system

FRC 2022 Team 5468's 2022 FRC robot code. This code is written in Java and is based off of WPILib's Java control system and utilizes a command based s

null 4 Oct 4, 2022
ijrd - internal java runtime debugger (loads through java agents LOL)

ijrd ijrd - internal java runtime debugger (loads through java agents LOL) this actually requires brain to build and then setup little guide to setup

null 6 Jan 28, 2022
Java - Packet Analyzer Application based on Java, Networking and Swing UI

Network-Packet-Tracer-using-Java Java - Packet Analyzer / Sniffing System Application based on Java, Networking and Swing UI Java - Packet Analyzer Ap

Muhammad Asad 6 Feb 3, 2022
Java virtual machine made in Java by HoverCatz#0060

Obzcure Virtual Machine Java virtual machine made in Java THIS IS NOT PRODUCTION SAFE - WORK IN PROGRESS! Use at your own risk. Requires Java 17 (with

null 9 Oct 18, 2022
Create a Music Playlist Library -Core JAVA, JAVA Swing, AWT

Project Specifications Manage Everything about a basic Music Playlist Application in Java A Music Library for Listing, Replaying, Navigating between c

Muhammad Asad 7 Nov 8, 2022
This repository contains Java programs to become zero to hero in Java. Programs related to each and every concep are present from easy to intermidiate level.

Learn Java Programming In this repository you will find topic wise programs of java from basics to intermediate. This follows topic wise approach that

Sahil Batra 15 Oct 9, 2022
Alibaba Cloud Dedicated KMS Transfer SDK for Java can help Java developers to migrate from the KMS keys to the Dedicated KMS keys.

Alibaba Cloud Dedicated KMS Transfer SDK for Java Alibaba Cloud Dedicated KMS Transfer SDK for Java can help Java developers to migrate from the KMS k

Alibaba Cloud 3 May 12, 2022
Resconstruct is a java library to infer missing information vectors of java classes.

Reconstruct Resconstruct is a java library to infer missing information vectors of java classes. Features Phantom classes Inheritance solving Dummy fi

Nowilltolife 14 Nov 17, 2022