diff options
author | ArghKevin <kagheli@student.sdccd.edu> | 2024-05-16 03:13:50 -0700 |
---|---|---|
committer | ArghKevin <kagheli@student.sdccd.edu> | 2024-05-16 03:13:50 -0700 |
commit | 4a88de54721a6b11220d9b5a747f936e995941e3 (patch) | |
tree | 48f6ba4704bdd023b7cd195629b6cc38d353b3fd /src/JSONReader.java | |
parent | 03991b0cabc7a510e0d2ff7ed1a2f0bbc300e7c1 (diff) |
Simple JSON parsing. Will need extension for tables.
Diffstat (limited to 'src/JSONReader.java')
-rw-r--r-- | src/JSONReader.java | 70 |
1 files changed, 66 insertions, 4 deletions
diff --git a/src/JSONReader.java b/src/JSONReader.java index 8d670b6..45f6bd5 100644 --- a/src/JSONReader.java +++ b/src/JSONReader.java @@ -1,4 +1,5 @@ import java.util.HashMap; +import java.util.ArrayList; import java.io.*; /* @@ -6,6 +7,8 @@ import java.io.*; * Kian Agheli * * References: + * https://googlefonts.github.io/gf-guide/metadata.html + * https://stackoverflow.com/questions/3880274/how-to-convert-the-object-to-string-in-java * * Date: * 2024-05-08 @@ -15,23 +18,75 @@ import java.io.*; */ public class JSONReader extends Reader { + /* metadata.pb is not exactly JSON, but so close to it + that it's practically a subset of JSON with fewer quotation + marks. If set, this is a pb file. */ + private boolean pb; + public JSONReader(File file) { super(file); + pb = false; + } + + private String composeKey(String key) { + if (!pb) { + key = "\"" + key + "\""; + } + key += ": "; + + return key; } /** * Search for and return the first * match. */ - public String get(String field) { - return null; + public String get(String key) { + key = composeKey(key); + + int start = this.getContents().indexOf(key); + if (start == -1) { + /* No match. */ + return null; + } + + start += key.length(); + /* This JSON is pretty-printed. */ + int nl = this.getContents().indexOf("\n", start); + + /* Extract substring. Remove any quotation marks and commas. */ + String substring = this.getContents().substring(start, nl); + return substring.replaceAll("[\",]", ""); } /** * Search for and return all matches. */ - public String[] getAll(String field) { - return null; + public String[] getAll(String key) { + key = composeKey(key); + + ArrayList<String> list = new ArrayList<String>(); + int lastMatch = 0; + while ((lastMatch = this.getContents().indexOf(key, lastMatch)) != -1) { + lastMatch += key.length(); + int start = lastMatch; + /* This JSON is pretty-printed. */ + int nl = this.getContents().indexOf("\n", start); + if (nl == -1) { + /* EOF. */ + nl = this.getContents().length(); + } + + /* Extract substring. Remove any quotation marks and commas. */ + String substring = this.getContents().substring(start, nl); + list.add(substring.replaceAll("[\",]", "")); + } + + /* The caller doesn't need the capabilites of an ArrayList, only + those of an array. */ + String[] array = new String[list.size()]; + list.toArray(array); + return array; } /** @@ -40,4 +95,11 @@ public class JSONReader extends Reader { public String getTable(HashMap<String,String> name) { return null; } + + /** + * Specify that this is the special PB format. + * See https://googlefonts.github.io/gf-guide/metadata.html. */ + public void setPb() { + this.pb = true; + } } |