Git Product home page Git Product logo

minimal-json's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

minimal-json's Issues

Shouldn't take object properties order into consideration

I liked your library a lot, much simpler than most of the others. I found that a test here failed because the "id" property was in the end of the object instead of the beginning. According to json.org: "An object is an unordered set of name/value pairs".

Can't iterate over type: class com.eclipsesource.json.JsonArray

Hi,

In the readme I see that it should be possible to iteratie over the elements of a JsonArray, but when I try that I get an error "Can't iterate over type: class com.eclipsesource.json.JsonArray".

I'm using JMeter 2.13 BeanShell Postprocessor and used the example from http://eclipsesource.com/blogs/2014/06/12/parsing-json-responses-with-jmeter/

The scripts looks like this:

import com.eclipsesource.json.*;

String jsonString = prev.getResponseDataAsString();
JsonObject family = JsonObject.readFrom(jsonString);
JsonArray members = family.get("members").asArray();

for (JsonValue member : members) {
  // ...
}

The for-loops gives an error.
What am I doing wrong?

jsonvalue.cutOffPointZero(String s)

When setting values to floating point numbers which end on ".0" like this
jsonobject.set("test", (double) 21.0)
then the number is automatically truncated to an integer: {"test":21}
If I set a double value then I would expect the result to be a double and not an integer.
I had to remove this method in my project to be able to differ between integer/long and float/double.
Is this method essential in some way?

Support modify Values

I think there should be a method which supports modifying the content of a value.
For example, when I have a array with 5 entries and I want to change the 3rd one. I have to make a 'temp' array where I add() entry 1 to 2 then add() a modified entry 3 and after that add the last 2 entries.
Now I have to remove() the array which I want to modify and add() the modidied Array.

This would be much easier when there is a replace() method because then I can directly modify entry number 3 and thats all.

Hope you understood what I mean :D

Cannot use 0.9.3 from maven central

Perhaps it's my fault, but I have changes my pom with 0.9.3:

<dependency>
    <groupId>com.eclipsesource.minimal-json</groupId>
    <artifactId>minimal-json</artifactId>
    <version>0.9.3</version>
</dependency

, and I have the following errors :

[ERROR] Failed to execute goal on project _a: Could not resolve dependencies for project __a:_a:jar:0.0.1-SNAPSHOT: 
Failed to collect dependencies at com.eclipsesource.minimal-json:minimal-json:jar:0.9.3: Failed to read artifact descriptor for com.eclipsesource.minimal-json:minimal-json:jar:0.9.3: 
Could not find artifact com.eclipsesource.minimal-json:parent:pom:0.9.3 in central (http://repo.maven.apache.org/maven2) -> [Help 1]

Any help are welcome, thanks!

Incorrect exception type?

I like this library....but I found that:

JsonObject.readFrom(String) 's javadoc says that it throws an UnsupportedOperationException but it actually throws a com.eclipsesource.json.ParseException

Description

In JsonObject either the javadoc or the code is wrong.

the method's javadoc readFrom(String) says

   * @throws UnsupportedOperationException
   *           if the input does not contain a JSON object

However, if the input doesn't contain a JSON object it throws a com.eclipsesource.json.ParseException instead.

lines 126-139 in
https://github.com/ralfstx/minimal-json/blob/abf82149104ff3c70142ff1062e406cbd490da45/com.eclipsesource.json/src/main/java/com/eclipsesource/json/JsonObject.java

Seems like the javadoc and the actual exception being thrown don't match in other places.
Probably related to #8

How to reproduce it:

Running this unit test

package com.eclipsesource.json;

import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class JsonObjectReadFromTest {

    @Test(expected = java.lang.UnsupportedOperationException.class)
    public void JsonObjectReadFrom() {
        JsonObject jsonObject = JsonObject
                .readFrom("This is not a JSON object");

    }

    @Test
    public void parseExceptionIsUnsupportedOperationException() {
        Exception pex = new ParseException("", 0, 0, 0);
        assertTrue("ParseException is not an UnsupportedOperationException ",
                pex instanceof UnsupportedOperationException);
    }

}

The result is

java.lang.Exception: Unexpected exception, expected<java.lang.UnsupportedOperationException> but was<com.eclipsesource.json.ParseException>
    at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:28)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
....
    at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:19)
    ... 15 more

Mailing list?

Is there a user mailing list/forum for minimal-json?

Gradle

How can I install minimal-json library using Gradle?

Thank

add changelog

It would be good to have a changelog available, so users can see if upgrading makes sense to them - and what the latest stable release is...

Implement RFC7159 section-4

Currently minimal-json allows duplicated names, which is contrary to section 4 of RFC7159, which says: The names within an object SHOULD be unique.

Other parsers throw an exception. JSON in Java for example throws

org.json.JSONException: Duplicate key "status" at 
.... 

JSON Schema support

@ralfstx I'm developping a JSON Editor based on WTP https://github.com/angelozerr/eclipse-wtp-json (I will contribute to WTP soon).

I'm supporting JSON Schema for :

  • validation
  • completion.

by using JSON Schema at http://schemastore.org/

I have developed my own JSON Schema (works only for completion) because every JSON Schema written in Java doesn't support line number where there is an error. It's because the JSON source to validate doesn't contains line number, offset for each JsonValue.

I create this issue because I would liek to know if minimal-json could :

  • give line number, offset for each JsonValue (by default none line info, this feature will be avaiable with an option)
  • support a factory kind in teh JsonParser to create a JsonSchema instance instead of JsonObject instance.

If you are not aware to do that, close this issue and I will create my own minimal-json with those extension.

Provide static factory methods for JsonArray

Ideally, these factory methods should support varargs. Not sure about the method name.

JsonArray.createArray( 1, 2, 3 );

Or should the methods go to JsonValue to be next to valueOf( ... )?

JsonValue.createArray( "a", "b", "c" );

Or on a Util class like so:

Json.createJsonArray( 1, 2, 3 );

Up on maven?

Is this available on the online maven repository? I cannot find it.

Make it easier to run performancetest

I'm trying to run the benchmark. The performancetest subproject's README.md says: "To run a benchmark, use the benchmark's main method."

It took me a while before I realized I needed to run the command to copy all the JARs into "lib" and then launch things manually:

# java -cp target/classes:$(ls lib/*.jar | tr ' ' ':') com.eclipsesource.json.performancetest.ReadWriteBenchmark

Two ways to make life easier:

  1. Make things work with "mvn exec:java" (see "mvn exec:java doesn't work", below). That's the first thing I tried. This would also eliminate the need to copy all the JARs into "lib/".
  2. Provide a shell script to run things. Also, instead of copying all the JARs into "lib/", you can use the "maven-dependency-plugin" to reference the JARs wherever they currently are (see "building classpath without copying", below)

mvn exec:java doesn't work

# mvn exec:java -Dexec.mainClass="com.eclipsesource.json.performancetest.ReadWriteBenchmark"
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building minimal-json performance tests 0.9.2-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.3.2:java (default-cli) @ minimal-json-performancetest ---
[WARNING] Warning: killAfter is now deprecated. Do you need it ? Please comment on MEXEC-6.
 0% Scenario{vm=java, trial=0, benchmark=ReadFromString, input=rap, parser=org-json}  Failed to execute java -cp /usr/local/Cellar/maven/3.2.3/libexec/boot/plexus-classworlds-2.5.1.jar com.google.caliper.InProcessRunner --warmupMillis 3000 --runMillis 1000 --measurementType TIME --marker //ZxJ/ -Dbenchmark=ReadFromString -Dinput=rap -Dparser=org-json com.eclipsesource.json.performancetest.ReadWriteBenchmark
Error: Could not find or load main class com.google.caliper.InProcessRunner

An exception was thrown from the benchmark code.
com.google.caliper.ConfigurationException: Failed to execute java -cp /usr/local/Cellar/maven/3.2.3/libexec/boot/plexus-classworlds-2.5.1.jar com.google.caliper.InProcessRunner --warmupMillis 3000 --runMillis 1000 --measurementType TIME --marker //ZxJ/ -Dbenchmark=ReadFromString -Dinput=rap -Dparser=org-json com.eclipsesource.json.performancetest.ReadWriteBenchmark
    at com.google.caliper.Runner.measure(Runner.java:309)
    at com.google.caliper.Runner.runScenario(Runner.java:229)
    at com.google.caliper.Runner.runOutOfProcess(Runner.java:378)
    at com.google.caliper.Runner.run(Runner.java:97)
    at com.eclipsesource.json.performancetest.caliper.CaliperRunner.safeRun(CaliperRunner.java:114)
    at com.eclipsesource.json.performancetest.caliper.CaliperRunner.exec(CaliperRunner.java:57)
    at com.eclipsesource.json.performancetest.ReadWriteBenchmark.main(ReadWriteBenchmark.java:131)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:293)
    at java.lang.Thread.run(Thread.java:745)

Is Caliper trying to do runtime class loading?

Building classpath without copying

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <configuration>
                        <outputFile>${project.build.directory}/runtime-classpath</outputFile>
                        <includeScope>runtime</includeScope>
                    </configuration>
                    <executions>
                        <execution>
                            <phase>compile</phase>
                            <goals>
                                <goal>build-classpath</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

This way, a helper shell script can just do "-cp $(cat target/runtime-classpath)", eliminating the JAR copying step.

Support converting POJO => JSON

right now, the conversion seems to be just only one-way, i.e. JSON string to custom Java object (JsonValue, JsonObject, ...)

jsonvalue.isInteger()

Why are methods like isInteger() missing? Are simplicity and minimalism the reasons?
Right now there is only isNumber() so I use

jsonvalue.toString().contains(".")

additionally as a workaround to check whether the number is an "integer" or not.

Add to doc code to parse both object and array json string

Hi,

I was looking for a way to parse both json object or array and test afterward which one it is. I found a way but I think it should be added to the readme doc. Here is what I did:

    try{
        JsonValue value = JsonValue.readFrom(jsonString);
        if (value instanceof JsonObject) {
            object = (JsonObject) value;
        } else {
            array = (JsonArray) value;
        }
    }catch(RuntimeException e){
        // process error
    }

minimal-json 0.9.3 maven dependency not usable

Hi there.

With version 0.9.3 which is available in maven central:
http://search.maven.org/#artifactdetails|com.eclipsesource.minimal-json|minimal-json|0.9.3|bundle

it seems there was a maven parent pom introduced.
But this parent pom is not available in Maven central, so when using minimal-json 0.9.3 my build fails with:

Failed to collect dependencies at com.eclipsesource.minimal-json:minimal-json:jar:0.9.3: Failed to read artifact descriptor for com.eclipsesource.minimal-json:minimal-json:jar:0.9.3: Could not find artifact com.eclipsesource.minimal-json:parent:pom:0.9.3

Revise behavior for duplicate object members

JsonObject does not prevent duplicate names, because RFC 4627 does not. For example, it is possible to create a JsonObject for { "a": 1, "a": 2 }.

In case of duplicate names in an object, the behavior of the methods get() and remove() should be revised. The current behavior is that get( name ) returns the value of the first matching member, and also remove( name ) deletes only the first one.

It seems more intuitive that a member “shadows” previous members with the same name. Following this logic, get( name ) should always return the last member with this name. This would also ensure that calling get( name ) directly after add( name, value ) will always return value.

For remove( name ) there seem to be different options:

  • be consistent with get( name ) and remove the last member with this name. This would ensure that calling remove( name ) directly after add( name, value ) would revert the addition and leave the object in the previous state.
  • remove all members with this name. This would ensure that calling get( name ) directly after remove( name ) will always return null regardless of the previous state.

StackOverflowError when Json.parse()'ing a deeply nested JSON

Hi.
We found out that when parsing a very (very very) deeply nested JSON Object (~ 4.000 levels) with Json.parse(String) minimaljson runs out of "Stack" with a java.lang.StackOverflowError (which is quite understandable) :)
Of course this is no "use case" for us - it just came up during a pentest which then caused a JVM crash.

Would it be possible to add a configuration to the JsonParser which configures a maximum nesting level?
And then throwing a ParseException when parsing a too deeply nested JSON-String.

Using minimal-json without StringWriter

I am new to github and not sure if I am in correct place here??
I am developing a web project and I am using TeaVM an ahead-of-time translating compiler (transpiler) from Java bytecode to JavaScript , and it has some restriction of some parts of JDK can used in TeaVM for example java.lang.reflect not fully supported
and among not supported classes is java.io.StringWriter witch used by minimal-json so is it possible to get a version of minimal-json can run under this environment .thanks

Support creating JSON numbers from custom types like BigDecimal

Making JsonNumber public would allow to create JSON numbers that does not fit into any of the primitive Java types. Examples would be numbers with arbitrary precision, e.g. BigInteger and BigDecimal. Alternatively, factory methods could be offered for these two types.

Introduce class Json for constants and static factory methods?

With the constants NULL, TRUE, and FALSE defined on JsonValue, these constants are inherited by the subtypes of JsonValue. For example, the expression JsonArray.TRUE would compile although it doesn't make any sense. The same is true for static factory methods. The expression JsonArray.valueOf(true) would also compile and return JsonValue.TRUE which is not an instance of JsonArray.

Moving these constants and factory methods to a dedicated class Json would solve these issues. Moreover, it would result in shorter and more concise code:

Json.NULL, Json.TRUE, etc. instead of JsonValue.NULL, JsonValue.TRUE.

We could consider to rename the factory methods to value instead of valueOf.

Json.value(true), Json.value(23)...

This would also allow to add factory methods for arrays such as:

Json.array(23, 42), etc.

IOOBE after JsonObject.remove

The following code

JsonObject object = JsonObject.readFrom( "{ \"globals\": 23, \"undef\": true }" );
object.remove( "globals" );
object.get( "indent" );

leads to

java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
    at java.util.ArrayList.RangeCheck(ArrayList.java:546)
    at java.util.ArrayList.get(ArrayList.java:321)
    at com.eclipsesource.json.JsonObject.indexOf(JsonObject.java:617)
    at com.eclipsesource.json.JsonObject.get(JsonObject.java:516)
...

JsonWriter with indent

It should be cool if we could customize the JsonWriter to indent JSON.

An indent option could be added in the JsonValue#writeTo(Writer writer, Integer indent) to indent the JSON stream.

Provide streaming API for reading

It should be possible to

  • register a handler that receives callbacks during parsing
  • avoid creating an object representation of the entire input in this mode
  • interrupt the parsing at any point

Retrieving line of JsonValue in file?

Is it possible to find the line of a JsonValue in a file? I'd like to throw an exception relating to an invalid field value and it would be nice if I could provide the line of the issue, also meaning I can retrieve the actual text itself to include in the exception.

It would be useful for generating exceptions like this:

Failed to parse JSON!
"myValue" can't be null on line 7:
    ...
    "myValue": null,
    ...

Weird null behaviour

When parsing {"abc":null}

jsonObject.getLong("abc",0);

throws a NumberFormatException because the value is somehow converted to "null"

Unsure if we're supposed to use jsonObject.get().isNull() or if we could enhance the method below to do the null checking instead.

public JsonValue get(String name) {
        if(name == null) {
            throw new NullPointerException("name is null");
        } else {
            int index = this.indexOf(name);
           // if NullLiteral.equals(this.values.get(index)) ??
            return index != -1?(JsonValue)this.values.get(index):null;
        }
    }

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.