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

Commit

Permalink
1.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Aidan Follestad committed Feb 23, 2017
1 parent f721355 commit 44a5c92
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 42 deletions.
79 changes: 55 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ in the Android SDK. As we all know, those stock classes tend to be a pain. They
1. [Dependency](https://github.com/afollestad/ason#dependency)
1. [Gradle (Java)](https://github.com/afollestad/ason#gradle-java)
2. [Gradle (Android)](https://github.com/afollestad/ason#gradle-android)
3. [Maven](https://github.com/afollestad/ason#maven)
3. [Gradle (Kotlin)](https://github.com/afollestad/ason#gradle-kotlin)
4. [Maven](https://github.com/afollestad/ason#maven)
2. [Parsing and Building Objects](https://github.com/afollestad/ason#parsing-and-building-objects)
3. [Retrieving Values from Objects](https://github.com/afollestad/ason#retrieving-values-from-objects)
4. [Parsing and Building Arrays](https://github.com/afollestad/ason#parsing-and-building-arrays)
Expand Down Expand Up @@ -52,7 +53,7 @@ The dependency is available via jCenter.
```Gradle
dependencies {
...
compile 'com.afollestad:ason:1.3.1'
compile 'com.afollestad:ason:1.4.0'
}
```

Expand All @@ -63,9 +64,22 @@ Since Android includes `org.json` classes, you'll want to exclude the copies pro
```Gradle
dependencies {
...
compile('com.afollestad:ason:1.3.1') {
compile('com.afollestad:ason:1.4.0') {
exclude group: 'org.json', module: 'json'
// exclude group: 'com.intellij', module: 'annotations' - Enable this, if you use Kotlin, otherwise you may get a DexException
}
}
```

### Gradle (Kotlin)

In Kotlin, you'll want to exclude IntelliJ's annotations library to avoid a DexException. *If you are using Kotlin with
Android, make sure you also exclude org.json as shown in the section above.*

```Gradle
dependencies {
...
compile('com.afollestad:ason:1.4.0') {
exclude group: 'com.intellij', module: 'annotations'
}
}
```
Expand All @@ -76,7 +90,7 @@ dependencies {
<dependency>
<groupId>com.afollestad</groupId>
<artifactId>ason</artifactId>
<version>1.3.1</version>
<version>1.4.0</version>
<type>pom</type>
</dependency>
```
Expand Down Expand Up @@ -288,13 +302,24 @@ int day = ason.get("birthday.day");
int year = ason.get("birthday.year");
```

You can do the same on arrays, but you need to specify the index of the object to pull from too:
If you wanted to remove the inner "year" value:

```java
Ason ason = // ...
ason.remove("birthday.year");
```

---

You can use dot notation with arrays too, but you need to specify the index of the object to pull from:

```java
AsonArray ason = // ...
String name = ason.get(1, "birthday.month");
```

---

As a bonus, you can check equality without doing a manual value comparison:

```java
Expand All @@ -305,22 +330,6 @@ AsonArray ason2 = // ...
boolean birthYearCheck2 = ason2.equal(2, "birthday.year", 1995);
```

And arrays:

```java
Ason ason = new Ason()
.put("id", 1)
.put("name", "Aidan")
.put("birthday.month", "July")
.put("birthday.day", 28)
.put("birthday.year", 1995);
AsonArray<Ason> array = AsonArray<Ason>();
array.put(ason);

// The first parameter is the index of the item, the second is a key path, the third is the value you're comparing to
boolean firstItemBirthYearCheck = array.equal(0, "birthday.year", 1995);
```

### Index Notation

To extend on dot notations in paths, you can use this notation to perform operations on array children.
Expand All @@ -344,15 +353,37 @@ Take this JSON:
}
```

You could create this using index notation as such:

```java
Ason ason = new Ason()
.put("group_id", 1)
.put("title", "Hello, world!")
.put("participants.$0.name", "Aidan")
.put("participants.$0.id", 2)
.put("participants.$1.name", "Waverly")
.put("participants.$1.id", 1);
```

The dollar sign followed by the number 0 indicates that you want the item at index 0 (position 1)
within an array called "participants".

---

You can retrieve the value of "name" in the second participant like this:

```java
Ason object = // ...
String name = object.get("participants.$1.name");
```

The dollar sign followed by the number 1 indicates that you want the item at index 1 (position 2)
within the array called "participants".
If you wanted to remove the first item from the inner array, you can do that with index notation. This avoids the
need to first retrieve the "participants" object:

```java
Ason object = // ...
object.remove("participants.$0");
```

### Escaping Periods and Dollar Signs

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 '1.3.1'
version '1.4.0'

apply plugin: 'java'
apply plugin: 'idea'
Expand Down Expand Up @@ -32,7 +32,7 @@ publish {
userOrg = 'drummer-aidan'
groupId = 'com.afollestad'
artifactId = 'ason'
publishVersion = '1.3.1'
publishVersion = '1.4.0'
website = 'https://github.com/afollestad/ason'
}

Expand Down
29 changes: 26 additions & 3 deletions src/main/java/com/afollestad/ason/Ason.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,39 @@ public Ason put(@NotNull String key, Object... values) {
}
if (key.contains(".")) {
final String[] splitKey = splitPath(key);
JSONObject target = followPath(json, key, splitKey, true);
target.put(splitKey[splitKey.length - 1], insertObject);
Object target = followPath(json, key, splitKey, true);
if (target instanceof JSONArray) {
JSONArray arrayTarget = (JSONArray) target;
String indexKey = splitKey[splitKey.length - 1].substring(1);
int insertIndex = Integer.parseInt(indexKey);
if (insertIndex > arrayTarget.length() - 1) {
arrayTarget.put(insertObject);
} else {
arrayTarget.put(insertIndex, insertObject);
}
} else {
((JSONObject) target).put(splitKey[splitKey.length - 1], insertObject);
}
} else {
putInternal(null, null, key, insertObject);
}
return this;
}

public Ason remove(@NotNull String key) {
json.remove(key);
String[] splitKey = splitPath(key);
if (splitKey.length == 1) {
json.remove(key);
} else {
Object followed = followPath(json, key, splitKey, false);
if (followed instanceof JSONArray) {
JSONArray followedArray = (JSONArray) followed;
int insertIndex = Integer.parseInt(splitKey[splitKey.length - 1].substring(1));
followedArray.remove(insertIndex);
} else {
((JSONObject) followed).remove(splitKey[splitKey.length - 1]);
}
}
return this;
}

Expand Down
47 changes: 35 additions & 12 deletions src/main/java/com/afollestad/ason/Util.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.afollestad.ason;

import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONObject;

Expand Down Expand Up @@ -35,7 +36,7 @@ static String[] splitPath(String key) {
return result.toArray(new String[result.size()]);
}

private static boolean isNumber(String string) {
static boolean isNumber(String string) {
for (char c : string.toCharArray()) {
if (!Character.isDigit(c)) {
return false;
Expand All @@ -44,10 +45,11 @@ private static boolean isNumber(String string) {
return true;
}

static JSONObject followPath(JSONObject wrapper,
String key,
String[] splitKey,
boolean createMissing) {
@NotNull static Object followPath(
JSONObject wrapper,
String key,
String[] splitKey,
boolean createMissing) {
// Get value for the first path key
Object parent = wrapper.opt(splitKey[0]);
if (parent != null
Expand All @@ -58,7 +60,13 @@ static JSONObject followPath(JSONObject wrapper,
parent.getClass().getName() + ").");
} else if (parent == null) {
if (createMissing) {
parent = new JSONObject();
if (splitKey[0].startsWith("$")
|| (splitKey.length > 1
&& splitKey[1].startsWith("$"))) {
parent = new JSONArray();
} else {
parent = new JSONObject();
}
wrapper.put(splitKey[0], parent);
} else {
throw new InvalidPathException("No object or array found for the first component of key " +
Expand All @@ -85,8 +93,14 @@ static JSONObject followPath(JSONObject wrapper,
current.getClass().getName() + ").");
} else if (current == null) {
if (createMissing) {
current = new JSONObject();
((JSONObject) parent).put(currentKey, current);
if (i < splitKey.length - 1
&& splitKey[i + 1].startsWith("$")) {
current = new JSONArray();
((JSONArray) parent).put(current);
} else {
current = new JSONObject();
((JSONArray) parent).put(current);
}
} else {
throw new NullPathException("Item at index " + i + " " +
"of current entry refers to a null or out of bounds entry.");
Expand All @@ -107,7 +121,12 @@ static JSONObject followPath(JSONObject wrapper,
current.getClass().getName() + ").");
} else if (current == null) {
if (createMissing) {
current = new JSONObject();
if (i < splitKey.length - 1
&& splitKey[i + 1].startsWith("$")) {
current = new JSONArray();
} else {
current = new JSONObject();
}
((JSONObject) parent).put(currentKey, current);
} else {
throw new NullPathException("Item at index " + i + " " +
Expand All @@ -117,7 +136,7 @@ static JSONObject followPath(JSONObject wrapper,
parent = current;
}

return (JSONObject) parent;
return parent;
}

@SuppressWarnings("unchecked") static <T> T getPathValue(
Expand All @@ -127,8 +146,12 @@ static JSONObject followPath(JSONObject wrapper,
if (splitKey.length == 1) {
return (T) wrapper.get(key);
}
JSONObject target = followPath(wrapper, key, splitKey, false);
return (T) target.opt(splitKey[splitKey.length - 1]);
Object target = followPath(wrapper, key, splitKey, false);
if (target instanceof JSONObject) {
return (T) ((JSONObject) target).opt(splitKey[splitKey.length - 1]);
} else {
throw new InvalidPathException("Cannot get a value from a JSONArray using a key.");
}
}

@SuppressWarnings("unchecked")
Expand Down
Loading

0 comments on commit 44a5c92

Please sign in to comment.