Skip to content

Commit

Permalink
bring over json changes
Browse files Browse the repository at this point in the history
  • Loading branch information
lavajuno committed Jan 15, 2024
1 parent e574d50 commit d896765
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 82 deletions.
75 changes: 0 additions & 75 deletions src/main/java/org/lavajuno/lucidjson/Json.java

This file was deleted.

46 changes: 46 additions & 0 deletions src/main/java/org/lavajuno/lucidjson/JsonArray.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package org.lavajuno.lucidjson;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.util.List;
import java.util.Scanner;
import java.util.Vector;

/**
Expand Down Expand Up @@ -31,6 +35,48 @@ protected JsonArray(String text) throws ParseException {
values = parseValues(text.strip());
}

/**
* Deserializes a JSON array from a String.
* @param text Input string
* @return Deserialized JSON array
* @throws ParseException if parsing fails;
*/
public static JsonArray from(String text) throws ParseException {
String line = text.replace("\n", "");
if(!line.matches(ARRAY_RGX)) {
printError(line, "Expected an array.");
throw new ParseException("Expected an array.", 0);
}
return new JsonArray(line);
}

/**
* Deserializes a JSON array from a list of lines (Strings).
* @param lines Input lines
* @return Deserialized JSON array
* @throws ParseException If parsing fails
*/
public static JsonArray from(List<String> lines) throws ParseException {
StringBuilder sb = new StringBuilder();
for(String i : lines) { sb.append(i); }
return from(sb.toString());
}

/**
* Deserializes a JSON array from a file.
* @param file_path Path to the input file
* @return Deserialized JSON array
* @throws FileNotFoundException If the file could not be read
* @throws ParseException If parsing fails
*/
public static JsonArray fromFile(String file_path) throws FileNotFoundException, ParseException {
Scanner file = new Scanner(new FileInputStream(file_path));
StringBuilder lines = new StringBuilder();
while(file.hasNextLine()) { lines.append(file.nextLine()); }
file.close();
return from(lines.toString());
}

/**
* @param text JSON to parse
* @return Vector created from the input
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/org/lavajuno/lucidjson/JsonEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,22 @@ protected static Pair<String, JsonEntity> parsePair(String text) throws ParseExc
/**
* Serializes this JsonEntity to a String with newlines and indentation.
* @param indent Indent of this JsonEntity
* @return This JsonEntity as a string.
* @return This JsonEntity as a String
*/
protected abstract String toString(int indent);

/**
* Serializes this JsonEntity to a String with optional formatting.
* @param pretty Whether to use newlines and indents in the output
* @return This JsonEntity as a String
*/
public String toString(boolean pretty) {
return pretty ? this.toString(0) : this.toString();
}

/**
* Serializes this JsonEntity to a String without any formatting.
* @return This JsonEntity as a string.
* @return This JsonEntity as a String
*/
@Override
public abstract String toString();
Expand Down
50 changes: 46 additions & 4 deletions src/main/java/org/lavajuno/lucidjson/JsonObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import org.lavajuno.lucidjson.util.Pair;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.util.Collection;
import java.util.Set;
import java.util.TreeMap;
import java.util.Vector;
import java.util.*;

/**
* Represents a JSON object.
* Provides functionality for accessing and modifying its values.
* LucidJSON v0.0.1 (Experimental)
*/
@SuppressWarnings("unused")
public class JsonObject extends JsonEntity {
Expand All @@ -36,6 +36,48 @@ protected JsonObject(String text) throws ParseException {
values = parseValues(text.strip());
}

/**
* Deserializes a JSON object from a String.
* @param text Input string
* @return Deserialized JSON object
* @throws ParseException if parsing fails;
*/
public static JsonObject from(String text) throws ParseException {
String line = text.replace("\n", "");
if(!line.matches(OBJECT_RGX)) {
printError(line, "Expected an object.");
throw new ParseException("Expected an object.", 0);
}
return new JsonObject(line);
}

/**
* Deserializes a JSON object from a list of lines (Strings).
* @param lines Input lines
* @return Deserialized JSON object
* @throws ParseException If parsing fails
*/
public static JsonObject from(List<String> lines) throws ParseException {
StringBuilder sb = new StringBuilder();
for(String i : lines) { sb.append(i); }
return from(sb.toString());
}

/**
* Deserializes a JSON object from a file.
* @param file_path Path to the input file
* @return Deserialized JSON object
* @throws FileNotFoundException If the file could not be read
* @throws ParseException If parsing fails
*/
public static JsonObject fromFile(String file_path) throws FileNotFoundException, ParseException {
Scanner file = new Scanner(new FileInputStream(file_path));
StringBuilder lines = new StringBuilder();
while(file.hasNextLine()) { lines.append(file.nextLine()); }
file.close();
return from(lines.toString());
}

/**
* @param text JSON to parse
* @return Key-value map created from the input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class ApplicationConfig {
*/
public ApplicationConfig(String config_file_path) throws IOException, ParseException {
/* Parse configuration file */
final JsonObject config_root = Json.readFile(config_file_path);
final JsonObject config_root = JsonObject.fromFile(config_file_path);

/* Get configuration revision */
final JsonNumber config_revision = (JsonNumber) config_root.get("revision");
Expand Down

0 comments on commit d896765

Please sign in to comment.