I am creating a basic JSON string parser and I've run into a situation.
I've been using the following RFC.
RFC 8259: The JavaScript Object Notation (JSON) Data Interchange Format.
In Section 3 – Values, it defines that a value is as follows.
value = false / null / true / object / array / number / string
And, Section 4 – Objects defines an object.
object = begin-object [ member *( value-separator member ) ]
end-object
member = string name-separator value
Thus, in my code I created the following class structures.
private static class Value {
private java.lang.Object object;
@Override
public String toString() {
String string;
if (object instanceof Boolean) string = (boolean) object ? "true" : "false";
else if (object instanceof BigDecimal) string = ((BigDecimal) object).toPlainString();
else if (object instanceof String) string = (String) object;
else string = object.toString();
return string;
}
}
private static class Object {
List<Member> list = new ArrayList<>();
private static class Member {
private String name;
private List<Value> value;
@Override
public String toString() {
return "{" + name + ": " + value + "}";
}
}
@Override
public String toString() {
return list.toString();
}
}
I am attempting to determine if there is a more logical way to do this.
Is there a better way to contain each of those types, and then capture their content accordingly.
I thought of just storing them all as String values, although an object has less precedence than a value.
Therefore, there would not be any difference in what I currently have implemented.
I feel inheritance would prove abstruse and lacking, since it won't ever scale beyond this.
The values will always have the forms described.
Essentially, I want to have the following pseudo-code as an end-point.
And, the error I encounter is that the user will have to decouple an array of JSON objects.
To provide them with Java Objects seems daunting,
parser.get("key").asString();
parser.get(0).asArray();
parser.get(1).asObject();
parser.findObject("key").asObject();
parser.findNumber(123).asNumber();
Is there some sort of concept, or design, that can be used to harness a set of uncorrelated values?
How can I offer the JSON object, or array, to the user, in a way that won't require them to cast a Java Object?
Aucun commentaire:
Enregistrer un commentaire