Skip to content

Commit

Permalink
add ZsonKey
Browse files Browse the repository at this point in the history
definitely larger but at least get() works now
  • Loading branch information
rhysdh540 committed May 13, 2024
1 parent 832227c commit 743d866
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 18 deletions.
44 changes: 33 additions & 11 deletions zson/src/main/java/dev/nolij/zson/Zson.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package dev.nolij.zson;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.AbstractMap;
import java.util.Map;

Expand All @@ -15,23 +18,42 @@ public ZsonMap parse(String json) {
}

public String stringify(ZsonMap zson) {
StringBuilder sb = new StringBuilder("{\n");
StringWriter writer = new StringWriter();
try {
write(zson, writer);
} catch (IOException e) {
throw new RuntimeException(e);
}
return writer.toString();
}

public void write(ZsonMap zson, Writer writer) throws IOException {
writer.write("{\n");

for (var entry : zson.entrySet()) {
Map.Entry<String, String> keyPair = entry.getKey();
String key = keyPair.getKey(), comment = keyPair.getValue();
ZsonKey keyPair = entry.getKey();
String key = keyPair.key, comment = keyPair.comment;
Object value = entry.getValue();

if (comment != null && !comment.isEmpty()) {
if (comment != null) {
for (String line : comment.split("\n")) {
sb.append(indent).append("// ").append(line).append("\n");
writer.write(indent);
writer.write("// ");
writer.write(line);
writer.write("\n");
}
}

sb.append(indent).append('"').append(key).append("\": ").append(value(value)).append(",\n");
writer.write(indent);
writer.write('"');
writer.write(key);
writer.write("\": ");
writer.write(value(value));
writer.write(",\n");
}

return sb.append("}").toString();
writer.write("}");
writer.flush();
}

private String value(Object value) {
Expand All @@ -55,11 +77,11 @@ private String value(Object value) {
}
}

public static Map.Entry<Map.Entry<String, String>, Object> entry(String key, String comment, Object value) {
return new AbstractMap.SimpleEntry<>(new AbstractMap.SimpleEntry<>(key, comment), value);
public static Map.Entry<ZsonKey, Object> entry(String key, String comment, Object value) {
return new AbstractMap.SimpleEntry<>(new ZsonKey(key, comment), value);
}

public static Map.Entry<Map.Entry<String, String>, Object> entry(String key, Object value) {
return entry(key, "", value);
public static Map.Entry<ZsonKey, Object> entry(String key, Object value) {
return new AbstractMap.SimpleEntry<>(new ZsonKey(key), value);
}
}
20 changes: 20 additions & 0 deletions zson/src/main/java/dev/nolij/zson/ZsonKey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.nolij.zson;

public class ZsonKey {
public final String key;
public String comment;

public ZsonKey(String key, String comment) {
this.key = key;
this.comment = comment;
}

public ZsonKey(String key) {
this(key, null);
}

@Override
public int hashCode() {
return key.hashCode();
}
}
14 changes: 9 additions & 5 deletions zson/src/main/java/dev/nolij/zson/ZsonMap.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
package dev.nolij.zson;

import java.util.AbstractMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class ZsonMap extends LinkedHashMap<Map.Entry<String, String>, Object> {
public class ZsonMap extends LinkedHashMap<ZsonKey, Object> {
public ZsonMap() {
super();
}

@SafeVarargs
public ZsonMap(Map.Entry<Map.Entry<String, String>, Object>... entries) {
public ZsonMap(Map.Entry<ZsonKey, Object>... entries) {
super(entries.length);
for (var entry : entries) {
this.put(entry.getKey(), entry.getValue());
}
}

public ZsonMap put(String key, String comment, Object value) {
this.put(new AbstractMap.SimpleEntry<>(key, comment), value);
this.put(new ZsonKey(key, comment), value);
return this;
}

public ZsonMap put(String key, Object value) {
return this.put(key, "", value);
this.put(new ZsonKey(key), value);
return this;
}

public Object get(String key) {
return this.get(new ZsonKey(key));
}
}
5 changes: 3 additions & 2 deletions zson/src/test/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ public static void main(String[] args) throws Throwable {
Zson.entry("home", "217-555-1234"),
Zson.entry("cell", "217-555-5678")
));
Files.write(Paths.get("person.json5"), zson.stringify(zsonMap).getBytes(StandardCharsets.UTF_8));
String json = zson.stringify(zsonMap);
zson.write(zsonMap, Files.newBufferedWriter(Paths.get("person.json5"), StandardCharsets.UTF_8));
System.out.println(json);

ZsonMap map2 = new ZsonMap();
ZsonMap map3 = new ZsonMap();
map3.put("test", "a", map2);
map2.put("test", "b", map3);

System.out.println(zson.stringify(map2));
}
}

0 comments on commit 743d866

Please sign in to comment.