Skip to content
This repository has been archived by the owner on Jun 16, 2019. It is now read-only.

Commit

Permalink
0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
afollestad committed Feb 4, 2017
1 parent 5aadaae commit c2e42df
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .idea/modules/json.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/modules/json_main.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/modules/json_test.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 38 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ The dependency *will be available on jCenter shortly*, if it doesn't resolve yet
```Gradle
dependencies {
...
compile 'com.afollestad:json:0.1.0'
compile 'com.afollestad:json:0.1.1'
}
```

Expand All @@ -32,7 +32,7 @@ Since Android includes `org.json` classes, you'll want to exclude the copies pro
```Gradle
dependencies {
...
compile('com.afollestad:json:0.1.0') {
compile('com.afollestad:json:0.1.1') {
exclude group: 'org.json', module: 'json'
}
}
Expand All @@ -44,7 +44,7 @@ dependencies {
<dependency>
<groupId>com.afollestad</groupId>
<artifactId>json</artifactId>
<version>0.1.0</version>
<version>0.1.1</version>
<type>pom</type>
</dependency>
```
Expand Down Expand Up @@ -82,22 +82,49 @@ Json json = new Json()
.put("born", 1995);
```

You can quickly put in arrays just by passing multiple values to `put()`:

```java
// Translates to {"greetings":["Hello","Hey"]}
Json json = new Json();
// The first parameter is a key, you can pass any type for the rest of the varargs parameters
json.put("greetings", "Hello", "World");
```

---

# Retrieving Values from Objects

Various methods exist for retrieving existing values (default values are returned if they don't exist):
Various methods exist for retrieving existing values (default values are returned if they don't exist). The one parameter
version uses whatever the usual default of a type is (0 for number types, null for everything else), if the no value is found
for the key. The two parameter version lets you specify a custom default.

```java
Json json = // ...

String str = json.getString("name");
String strWithDefault = json.getString("name", null);

boolean bool = json.getBool("name");
boolean boolWithDefault = json.getBool("name", true);

short shrt = json.getShort("name");
short shrtWithDefault = json.getShort("name", (short)0);

int integer = json.getInt("name");
int integerWithDefault = json.getInt("name", 0);

long lng = json.getLong("name");
long lngWithDefault = json.getLong("name", 0L);

float flt = json.getFloat("name");
float fltWithDefault = json.getFloat("name", 0f);

double doub = json.getDouble("name");
double doubWithDefault = json.getDouble("name", 0d);

Json obj = json.getJsonObject("name");
JsonArray ary = json.getJsonArray("name");
```

Further, the `get(String)` method will actually automatically cast its return value to whatever variable you're setting it to:
Expand All @@ -107,8 +134,13 @@ String str = json.get("name");
long lng = json.get("name");
```

The only time you *need* to use explicit `get[Type]` getters is when you aren't setting them to a value before
using them (e.g. in an if statement).
It will also infer its type if you pass a default value, removing the need to use explicit `get[Type]` methods:

```java
if (json.get("name", false)) {
// do something
}
```

---

Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group 'com.afollestad'
version '0.1.0'
version '0.1.1'

apply plugin: 'java'

Expand Down Expand Up @@ -29,6 +29,6 @@ publish {
userOrg = 'drummer-aidan'
groupId = 'com.afollestad'
artifactId = 'json'
publishVersion = '0.1.0'
publishVersion = '0.1.1'
website = 'https://github.com/afollestad/json'
}
70 changes: 57 additions & 13 deletions src/main/java/com/afollestad/json/Json.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/**
* @author Aidan Follestad (afollestad)
*/
@SuppressWarnings({"WeakerAccess", "unused", "unchecked"}) public class Json {
@SuppressWarnings({"WeakerAccess", "unused", "unchecked", "SameParameterValue"}) public class Json {

private JSONObject json;
private JsonSerializer serializer;
Expand Down Expand Up @@ -111,7 +111,11 @@ public Json remove(String key) {
return this;
}

@SuppressWarnings("unchecked") public <T> T get(String key) {
public <T> T get(String key) {
return get(key, (T) null);
}

@SuppressWarnings("unchecked") public <T> T get(String key, T defaultValue) {
if (key == null)
throw new IllegalArgumentException("Key cannot be null.");
Object result;
Expand All @@ -122,7 +126,7 @@ public Json remove(String key) {
result = json.opt(key);
}
if (result == null) {
return null;
return defaultValue;
} else if (result instanceof JSONObject) {
result = new Json((JSONObject) result);
} else if (result instanceof JSONArray) {
Expand All @@ -136,37 +140,77 @@ public Json remove(String key) {
}

public boolean getBool(String key) {
return (Boolean) get(key);
return getBool(key, false);
}

public boolean getBool(String key, boolean defaultValue) {
return get(key, defaultValue);
}

public String getString(String key) {
return (String) get(key);
return getString(key, null);
}

public String getString(String key, String defaultValue) {
return get(key, defaultValue);
}

public short getShort(String key) {
return (Short) get(key);
return getShort(key, (short) 0);
}

public short getShort(String key, short defaultValue) {
return get(key, defaultValue);
}

public int getInt(String key) {
return (Integer) get(key);
return getInt(key, 0);
}

public int getInt(String key, int defaultValue) {
return get(key, defaultValue);
}

public long getLong(String key) {
return (Long) get(key);
return getLong(key, 0L);
}

public long getLong(String key, long defaultValue) {
return get(key, defaultValue);
}

public float getFloat(String key) {
return (Float) get(key);
return getFloat(key, 0f);
}

public float getFloat(String key, float defaultValue) {
return get(key, defaultValue);
}

public double getDouble(String key) {
return (Double) get(key);
return getDouble(key, 0d);
}

public double getDouble(String key, double defaultValue) {
return get(key, defaultValue);
}

public Json getJsonObject(String key) {
return get(key, (Json) null);
}

public JsonArray getJsonArray(String key) {
return get(key, (JsonArray) null);
}

public <T> T get(String key, Class<T> cls) {
return get(key, cls, null);
}

@SuppressWarnings("Duplicates") public <T> T get(String key, Class<T> cls) {
Object value = get(key);
@SuppressWarnings("Duplicates") public <T> T get(String key, Class<T> cls, T defaultValue) {
Object value = get(key, defaultValue);
if (value == null) {
return null;
return defaultValue;
} else if (isPrimitive(cls) ||
cls == JSONObject.class ||
cls == JSONArray.class ||
Expand Down
1 change: 1 addition & 0 deletions src/test/java/com/afollestad/json/JsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class JsonTest {
assertTrue(json.equal("name", "Aidan"));
assertTrue(json.equal("_id", 3));
assertTrue(json.equal("age", 21));
assertEquals(json.get("non-existent", 69).intValue(), 69);
}

@Test public void anon_fields_test() {
Expand Down

0 comments on commit c2e42df

Please sign in to comment.