Skip to content

Commit

Permalink
v2.0.0
Browse files Browse the repository at this point in the history
- fix quoteless string starting with punctuator
- add cli
  • Loading branch information
laktak committed Aug 1, 2016
1 parent c3e2973 commit dfbb4f5
Show file tree
Hide file tree
Showing 34 changed files with 310 additions and 68 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ gradle
gradlew
gradle.bat
build/
hjson.iml
.idea
5 changes: 5 additions & 0 deletions assets/charset_result.hjson
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
ql-ascii: ! "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
js-ascii: ! "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
ml-ascii: ! "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
}
5 changes: 5 additions & 0 deletions assets/charset_result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"ql-ascii": "! \"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~",
"js-ascii": "! \"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~",
"ml-ascii": "! \"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
}
6 changes: 6 additions & 0 deletions assets/charset_test.hjson
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ql-ascii: ! "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
js-ascii: "! \"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
ml-ascii:
'''
! "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
'''
12 changes: 12 additions & 0 deletions assets/comments_test.hjson
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
// test
# all
// comment
/*
styles
*/
# with lf



# !

{
# hjson style comment
foo1: This is a string value. # part of the string
Expand Down
3 changes: 3 additions & 0 deletions assets/empty_result.hjson
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"": empty
}
3 changes: 3 additions & 0 deletions assets/empty_result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"": "empty"
}
3 changes: 3 additions & 0 deletions assets/empty_test.hjson
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"": empty
}
2 changes: 2 additions & 0 deletions assets/fail34_test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
A quoteless string is OK,
but two must be contained in an array.
4 changes: 4 additions & 0 deletions assets/failStr1_test.hjson
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
# invalid quoteless string
ql: ]
}
4 changes: 4 additions & 0 deletions assets/failStr2_test.hjson
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
# invalid quoteless string
ql: }
}
4 changes: 4 additions & 0 deletions assets/failStr3_test.hjson
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
# invalid quoteless string
ql: {
}
4 changes: 4 additions & 0 deletions assets/failStr4_test.hjson
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
# invalid quoteless string
ql: [
}
4 changes: 4 additions & 0 deletions assets/failStr5_test.hjson
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
# invalid quoteless string
ql: :
}
4 changes: 4 additions & 0 deletions assets/failStr6_test.hjson
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
# invalid quoteless string
ql: ,
}
2 changes: 1 addition & 1 deletion assets/pass1_result.hjson
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
real: -9876.54321
e: 1.23456789e-13
E: 1.23456789e+34
"": 2.3456789012e+76
-: 2.3456789012e+76
zero: 0
one: 1
space: " "
Expand Down
2 changes: 1 addition & 1 deletion assets/pass1_result.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"real": -9876.54321,
"e": 1.23456789e-13,
"E": 1.23456789e+34,
"": 2.3456789012e+76,
"-": 2.3456789012e+76,
"zero": 0,
"one": 1,
"space": " ",
Expand Down
2 changes: 1 addition & 1 deletion assets/pass1_test.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"real": -9876.543210,
"e": 0.123456789e-12,
"E": 1.234567890E+34,
"": 23456789012E66,
"-": 23456789012E66,
"zero": 0,
"one": 1,
"space": " ",
Expand Down
2 changes: 1 addition & 1 deletion assets/passSingle_result.hjson
Original file line number Diff line number Diff line change
@@ -1 +1 @@
foo=bar
allow quoteless strings
2 changes: 1 addition & 1 deletion assets/passSingle_result.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"foo=bar"
"allow quoteless strings"
2 changes: 1 addition & 1 deletion assets/passSingle_test.hjson
Original file line number Diff line number Diff line change
@@ -1 +1 @@
foo=bar
allow quoteless strings
1 change: 0 additions & 1 deletion assets/strings_result.hjson
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,4 @@
two: "2"
minus: "-3"
}
"": empty
}
3 changes: 1 addition & 2 deletions assets/strings_result.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,5 @@
"one": "1",
"two": "2",
"minus": "-3"
},
"": "empty"
}
}
2 changes: 0 additions & 2 deletions assets/strings_test.hjson
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,4 @@
two: "2"
minus: "-3"
}

"": empty
}
54 changes: 54 additions & 0 deletions assets/testlist.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
charset_test.hjson
comments_test.hjson
empty_test.hjson
fail10_test.json
fail11_test.json
fail12_test.json
fail13_test.json
fail14_test.json
fail15_test.json
fail16_test.json
fail17_test.json
fail19_test.json
fail20_test.json
fail21_test.json
fail22_test.json
fail23_test.json
fail24_test.json
fail26_test.json
fail28_test.json
fail29_test.json
fail2_test.json
fail30_test.json
fail31_test.json
fail32_test.json
fail33_test.json
fail34_test.json
fail5_test.json
fail6_test.json
fail7_test.json
fail8_test.json
failKey1_test.hjson
failKey2_test.hjson
failKey3_test.hjson
failObj1_test.hjson
failObj2_test.hjson
failObj3_test.hjson
failStr1_test.hjson
failStr2_test.hjson
failStr3_test.hjson
failStr4_test.hjson
failStr5_test.hjson
failStr6_test.hjson
kan_test.hjson
keys_test.hjson
oa_test.hjson
pass1_test.json
pass2_test.json
pass3_test.json
pass4_test.json
passSingle_test.hjson
root_test.hjson
stringify1_test.hjson
strings_test.hjson
trail_test.hjson
12 changes: 10 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
apply plugin: 'java'

version = '1.1.4'
version = '2.0.0'
group = 'org.hjson'
description = """Hjson, the Human JSON."""

Expand Down Expand Up @@ -28,11 +28,19 @@ task javadocJar(type: Jar, dependsOn: javadoc) {
from 'build/docs/javadoc'
}

task sourcesJar(type: Jar, dependsOn:classes) {
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}

jar {
manifest {
attributes(
'Hjson-Version': version,
)
}
}

artifacts {
archives jar
archives sourcesJar
Expand Down
3 changes: 3 additions & 0 deletions cli/bin/hjson
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
SELF=`dirname $0`
java -jar $SELF/Hjson-cli.jar $*
2 changes: 2 additions & 0 deletions cli/bin/hjson.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@echo off
java -jar %~dp0\Hjson-cli.jar %*
69 changes: 69 additions & 0 deletions cli/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'application'

version = '2.0.0'
group = 'org.hjson.cli'
description = """Hjson CLI"""

sourceCompatibility = 1.8
targetCompatibility = 1.8

sourceSets {
main {
java {
srcDir 'src'
}
}
}

repositories {
flatDir {
dirs '../build/libs'
}
mavenCentral()
}

dependencies {
compile 'org.hjson:hjson:'+version
compile 'commons-cli:commons-cli:1.3.1'
}

mainClassName = 'org.hjson.cli.Main'

jar {
baseName = 'Hjson'
manifest {
attributes(
'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
'Main-Class': 'org.hjson.cli.Main',
'Hjson-Version': version,
)
}
}

task distjar(type: Jar) {
manifest=jar.manifest
archiveName="Hjson-cli.jar"
//baseName="Hjson-dist"
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar

}

task dist(type: Copy, dependsOn: distjar) {
from('build/libs') {
include '*.jar'
}
from('bin') {
include '*'
}
into 'build/target'
}

task zip(type:Zip, dependsOn: dist) {
from ('build/target')
include '*'
archiveName 'hjson.zip'
destinationDir file('build/zip')
}
76 changes: 76 additions & 0 deletions cli/src/org.hjson.cli/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.hjson.cli;

import org.hjson.*;
import java.io.*;
import static java.lang.System.out;
import org.apache.commons.cli.*;
import java.util.jar.Manifest;
import java.util.jar.Attributes;
import java.util.List;
import java.net.URL;

public class Main {

public static String convertStreamToString(InputStream is) throws IOException {
Writer writer=new StringWriter();
char[] buffer=new char[1024];
try {
Reader reader=new BufferedReader(new InputStreamReader(is, "UTF-8"));
int n;
while ((n=reader.read(buffer))!=-1) writer.write(buffer, 0, n);
} finally {
is.close();
}
return writer.toString();
}


public static void main(String[] args) throws Exception {

Options options=new Options();
options.addOption(OptionBuilder.withLongOpt("help").withDescription("Show this screen.").create("h"));
options.addOption(OptionBuilder.withDescription("Output as formatted JSON.").create("j"));
options.addOption(OptionBuilder.withDescription("Output as JSON.").create("c"));
options.addOption(OptionBuilder.withLongOpt("version").withDescription("Show version.").create("v"));

CommandLineParser parser=new DefaultParser();
HelpFormatter formatter=new HelpFormatter();
CommandLine cmd;

try {
cmd=parser.parse(options, args);
if (cmd.hasOption("h")) {
formatter.printHelp("hjson [INPUT]", options);
return;
} else if (cmd.hasOption("v")) {
String className=JsonObject.class.getSimpleName()+".class";
String classPath=JsonObject.class.getResource(className).toString();
if (!classPath.startsWith("jar")) return;
String manifestPath=classPath.substring(0, classPath.lastIndexOf("!")+1)+"/META-INF/MANIFEST.MF";
Attributes attr=new Manifest(new URL(manifestPath).openStream()).getMainAttributes();
out.println("hjson v"+attr.getValue("Hjson-Version"));
return;
}

List<String> cargs=cmd.getArgList();
Reader reader;

if (cargs.size()>1) throw new org.apache.commons.cli.ParseException("input");

if (cargs.isEmpty()) reader=new InputStreamReader(System.in);
else reader=new FileReader(cargs.get(0));
JsonValue value=JsonValue.readHjson(reader);

if (cmd.hasOption("j")) out.println(value.toString(Stringify.FORMATTED));
else if (cmd.hasOption("c")) out.println(value.toString(Stringify.PLAIN));
else out.println(value.toString(Stringify.HJSON));

} catch (org.apache.commons.cli.ParseException e) {
out.println(e.getMessage());
formatter.printHelp("hjson [INPUT]", options);
System.exit(1);
}


}
}
Loading

0 comments on commit dfbb4f5

Please sign in to comment.