Skip to content

Commit

Permalink
remove nullable annotations deps
Browse files Browse the repository at this point in the history
  • Loading branch information
AlmazKo committed May 19, 2020
1 parent e678083 commit 33e7798
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 24 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ JSON tiny library written in modern Java.
### Pros:
- Tiny library(3 small classes only!), ~5KB in a jar
- Fast JSON parsing, it faster than Jackson more than 25%, [benchmarks](https://github.com/AlmazKo/microjson/blob/master/src/jmh/results.txt)
- Bundled as java9 module without any JDK dependencies
- Bundled as Java 9 module without
- No dependencies, only java.base
- Support the full JSON specification

### Cons:
Expand Down
2 changes: 0 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ tasks.jacocoTestReport {


dependencies {
compileOnly("org.jetbrains:annotations:19.0.0")

"jmhImplementation"(project)
"jmhImplementation"("com.fasterxml.jackson.core:jackson-databind:2.11.0")
"jmhImplementation"("com.dslplatform:dsl-json-java8:1.9.5")
Expand Down
32 changes: 25 additions & 7 deletions src/main/java/almazko/microjson/JsObject.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package almazko.microjson;

import org.jetbrains.annotations.Nullable;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -18,23 +16,43 @@ private JsObject(Map<String, Object> values) {
this.values = new HashMap<>();
}

@Nullable public JsArray getArray(String key) {
/**
* @param key JSON field
* @return Array or {@code null}
*/
public JsArray getArray(String key) {
return (JsArray) values.get(key);
}

@Nullable public JsObject getObject(String key) {
/**
* @param key JSON field
* @return JsObject or {@code null}
*/
public JsObject getObject(String key) {
return (JsObject) values.get(key);
}

@Nullable public Number getNumber(String key) {
/**
* @param key JSON field
* @return Number or {@code null}
*/
public Number getNumber(String key) {
return (Number) values.get(key);
}

@Nullable public String getString(String key) {
/**
* @param key JSON field
* @return String or {@code null}
*/
public String getString(String key) {
return (String) values.get(key);
}

@Nullable public Boolean getBoolean(String key) {
/**
* @param key JSON field
* @return Boolean or {@code null}
*/
public Boolean getBoolean(String key) {
return (Boolean) values.get(key);
}

Expand Down
32 changes: 20 additions & 12 deletions src/main/java/almazko/microjson/Json.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package almazko.microjson;

import org.intellij.lang.annotations.Language;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.math.BigDecimal;
import java.math.BigInteger;

Expand Down Expand Up @@ -50,15 +46,15 @@ private void skipWhitespaces() {
}
}

private @NotNull String parseString() {
private String parseString() {
cursor++;
int endIdx = src.indexOf('"', cursor);
String value = src.substring(cursor, endIdx);
cursor = endIdx;
return value;
}

private @NotNull Number parseNumber() throws NumberFormatException {
private Number parseNumber() throws NumberFormatException {
boolean isInt = true;
final int beginIdx = cursor;

Expand All @@ -83,7 +79,7 @@ private void skipWhitespaces() {
}
}

private @NotNull JsObject parseObject() {
private JsObject parseObject() {
JsObject obj = null;
String key = null;

Expand All @@ -101,7 +97,11 @@ private void skipWhitespaces() {
}
}

private @NotNull JsArray parseArray() {

/**
* @return Array
*/
private JsArray parseArray() {
JsArray array = null;
for (cursor++; ; cursor++) {
char c = src.charAt(cursor);
Expand All @@ -120,11 +120,19 @@ private static boolean isWhitespace(char c) {
return c == ' ' || c == '\r' || c == '\n' || c == '\t';
}

public static @Nullable Object parse(@Language("JSON") String s) throws IllegalArgumentException {
return new Json(s).parseValue();
/**
* @param jsonContent JSON
* @return JSON element, including {@code null} when "null" is passed
*/
public static Object parse(String jsonContent) throws IllegalArgumentException {
return new Json(jsonContent).parseValue();
}

public static JsObject parseObject(@Language("JSON") String s) throws IllegalArgumentException {
return (JsObject) parse(s);
/**
* @param jsonContent JSON
* @return JSON object
*/
public static JsObject parseObject(String jsonContent) throws IllegalArgumentException {
return (JsObject) parse(jsonContent);
}
}
2 changes: 0 additions & 2 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
module microjson {
requires org.jetbrains.annotations;

exports almazko.microjson;
}
1 change: 1 addition & 0 deletions src/test/java/almazko/microjson/JsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ void parseObject() throws IOException {
}

@Test
//language=JSON
void parseArray() {
assertTrue(Json.parse("[1]") instanceof JsArray);
assertTrue(Json.parse("{}") instanceof JsObject);
Expand Down

0 comments on commit 33e7798

Please sign in to comment.