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

Commit

Permalink
Periods in paths can be escaped. 1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Aidan Follestad committed Feb 21, 2017
1 parent 0ed583c commit 6ae5bfd
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 27 deletions.
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ The dependency is available via jCenter.
```Gradle
dependencies {
...
compile 'com.afollestad:ason:1.1.0'
compile 'com.afollestad:ason:1.2.0'
}
```

Expand All @@ -60,7 +60,7 @@ Since Android includes `org.json` classes, you'll want to exclude the copies pro
```Gradle
dependencies {
...
compile('com.afollestad:ason:1.1.0') {
compile('com.afollestad:ason:1.2.0') {
exclude group: 'org.json', module: 'json'
}
}
Expand All @@ -72,7 +72,7 @@ dependencies {
<dependency>
<groupId>com.afollestad</groupId>
<artifactId>ason</artifactId>
<version>1.1.0</version>
<version>1.2.0</version>
<type>pom</type>
</dependency>
```
Expand Down Expand Up @@ -305,6 +305,23 @@ boolean firstItemBirthYearCheck = array.equal(0, "birthday.year", 1995);

---

If your keys actually have periods in them, you can escape periods:

```java
{
"files": {
"test.txt": "Hello, world!"
}
}
```

```java
Ason ason = // ...
String value = ason.get("files.test\\.txt");
```

---

# Serialization

This library allows very easy serialization and deserialization. Serialization is converting a Java class instance
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.1.0'
version '1.2.0'

apply plugin: 'java'
apply plugin: 'jacoco'
Expand Down Expand Up @@ -29,7 +29,7 @@ publish {
userOrg = 'drummer-aidan'
groupId = 'com.afollestad'
artifactId = 'ason'
publishVersion = '1.1.0'
publishVersion = '1.2.0'
website = 'https://github.com/afollestad/ason'
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/afollestad/ason/Ason.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ public Ason put(String key, Object... values) {
insertObject = newArray;
}
if (key.contains(".")) {
final String[] splitKey = key.split("\\.");
JSONObject target = Util.followPath(json, key, splitKey, true);
final String[] splitKey = splitPath(key);
JSONObject target = followPath(json, key, splitKey, true);
target.put(splitKey[splitKey.length - 1], insertObject);
} else {
putInternal(null, null, key, insertObject);
Expand All @@ -120,7 +120,7 @@ public <T> T get(String key) {
throw new IllegalArgumentException("Key cannot be null.");
Object result;
if (key.contains(".")) {
final String[] splitKey = key.split("\\.");
final String[] splitKey = splitPath(key);
result = getPathValue(json, key, splitKey);
} else {
result = json.opt(key);
Expand Down
11 changes: 4 additions & 7 deletions src/main/java/com/afollestad/ason/AsonArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
import java.util.Iterator;
import java.util.List;

import static com.afollestad.ason.Util.isList;
import static com.afollestad.ason.Util.isPrimitive;
import static com.afollestad.ason.Util.*;

/**
* @author Aidan Follestad (afollestad)
Expand Down Expand Up @@ -146,7 +145,7 @@ public boolean equal(int index, String path, Object value) {
} else {
encloser = ((Ason) arrayEntry).toStockJson();
}
Object pathValue = Util.getPathValue(encloser, path, path.split("\\."));
Object pathValue = getPathValue(encloser, path, splitPath(path));
if (pathValue == null) {
return value == null;
}
Expand All @@ -173,13 +172,11 @@ public JSONArray toStockJson() {
return array;
}

@Override
public Iterator<T> iterator() {
@Override public Iterator<T> iterator() {
return toList().iterator();
}

@Override
public String toString() {
@Override public String toString() {
return array.toString();
}

Expand Down
51 changes: 39 additions & 12 deletions src/main/java/com/afollestad/ason/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

Expand All @@ -15,41 +16,64 @@ class Util {
private Util() {
}

static String[] splitPath(String key) {
List<String> result = new ArrayList<>(4);
int start = 0;
for (int i = 0; i < key.length(); i++) {
if (key.charAt(i) == '.') {
if (i > 0 && key.charAt(i - 1) == '\\') {
continue;
}
String entry = key.substring(start, i)
.replace("\\.", ".");
result.add(entry);
start = i + 1;
}
}
result.add(key.substring(start).replace("\\.", "."));
return result.toArray(new String[result.size()]);
}

static JSONObject followPath(JSONObject encloser,
String key,
String[] splitKey,
boolean createMissing) {
Object parent = encloser.opt(splitKey[0]);
if (parent != null && !(parent instanceof JSONObject)) {
throw new InvalidPathException("First component of key " + key + " refers to " + splitKey[0] + ", which is not an object (it's a " + parent.getClass().getName() + ").");
throw new InvalidPathException("First component of key " + key + " refers to " +
splitKey[0] + ", which is not an object (it's a " + parent.getClass().getName() + ").");
} else if (parent == null) {
if (createMissing) {
parent = new JSONObject();
encloser.put(splitKey[0], parent);
} else {
throw new InvalidPathException("No object found for the first component of key " + key + " (" + splitKey[0] + ").");
throw new InvalidPathException("No object found for the first component of key " +
key + " (" + splitKey[0] + ").");
}
}
for (int i = 1; i < splitKey.length - 1; i++) {
Object current = ((JSONObject) parent).opt(splitKey[i]);
if (current != null && !(current instanceof JSONObject)) {
throw new InvalidPathException("Component " + (i + 1) + " of key " + key + " refers to " + splitKey[i] + ", which is not an object (most likely a primitive).");
throw new InvalidPathException("Component " + (i + 1) + " of key " + key +
" refers to " + splitKey[i] + ", which is not an object (most likely a primitive).");
} else if (current == null) {
if (createMissing) {
current = new JSONObject();
((JSONObject) parent).put(splitKey[i], current);
} else {
throw new InvalidPathException("Component " + (i + 1) + " of key " + key + " refers to " + splitKey[i] + ", which is not an object (most likely a primitive).");
throw new InvalidPathException("Component " + (i + 1) + " of key " + key +
" refers to " + splitKey[i] + ", which is not an object (most likely a primitive).");
}
}
parent = current;
}
return (JSONObject) parent;
}

@SuppressWarnings("unchecked") static <T> T getPathValue(JSONObject encloser,
String key,
String[] splitKey) {
@SuppressWarnings("unchecked") static <T> T getPathValue(
JSONObject encloser,
String key,
String[] splitKey) {
if (splitKey.length == 1) {
return (T) encloser.get(key);
}
Expand All @@ -58,8 +82,9 @@ static JSONObject followPath(JSONObject encloser,
}

@SuppressWarnings("unchecked")
static <T> T newInstance(Class<?> cls,
Map<Class<?>, Constructor<?>> cache) {
static <T> T newInstance(
Class<?> cls,
Map<Class<?>, Constructor<?>> cache) {
final Constructor ctor = getDefaultConstructor(cls, cache);
try {
return (T) ctor.newInstance();
Expand All @@ -68,8 +93,9 @@ static <T> T newInstance(Class<?> cls,
}
}

static Constructor<?> getDefaultConstructor(Class<?> cls,
Map<Class<?>, Constructor<?>> cache) {
private static Constructor<?> getDefaultConstructor(
Class<?> cls,
Map<Class<?>, Constructor<?>> cache) {
if (cache != null) {
Constructor ctor = cache.get(cls);
if (ctor != null) return ctor;
Expand Down Expand Up @@ -145,7 +171,8 @@ static void setFieldValue(Field field, Object object, Object value) {
try {
field.set(object, value);
} catch (IllegalAccessException e) {
throw new RuntimeException("Failed to set the value of " + field.getName() + " in " + object.getClass().getName(), e);
throw new RuntimeException("Failed to set the value of " + field.getName() + " in "
+ object.getClass().getName(), e);
}
}

Expand Down

0 comments on commit 6ae5bfd

Please sign in to comment.