
com.brettonw.bag.Base Maven / Gradle / Ivy
package com.brettonw.bag;
abstract class Base {
// data and functions for exporting as strings
static final String SQUARE_BRACKETS[] = { "[", "]" };
static final String CURLY_BRACKETS[] = { "{", "}" };
private static final String QUOTES[] = { "\"" };
String enclose (String input, String bracket[]) {
String bracket0 = bracket[0];
String bracket1 = (bracket.length > 1) ? bracket[1] : bracket0;
return bracket0 + input + bracket1;
}
String quote (String input) {
return enclose (input, QUOTES);
}
String getJsonString (Object object) {
if (object != null) {
switch (object.getClass ().getName ()) {
case "java.lang.String":
return quote ((String) object);
case "com.brettonw.bag.BagObject":
case "com.brettonw.bag.BagArray":
return ((Base) object).toJsonString ();
// we omit the default case, because there should not be any other types stored in
// the Bag class - as in, they would not make it into the container, as the
// "objectify" method will gate that
}
}
// if we stored a null, we need to emit it as a value. This will only happen in the
// array types, and is handled on the parsing side with a special case for reading
// the bare value 'null' (not quoted)
return "null";
}
Object objectify (Object value) {
if (value != null) {
Class type = value.getClass ();
String typeName = type.getName ();
switch (typeName) {
case "java.lang.String":
// is this the right place to do a transformation that converts quotes to some
// escape character?
return value;
case "java.lang.Long": case "java.lang.Integer": case "java.lang.Short": case "java.lang.Byte":
case "java.lang.Character":
case "java.lang.Boolean":
case "java.lang.Double": case "java.lang.Float":
return value.toString ();
case "com.brettonw.bag.BagObject":
case "com.brettonw.bag.BagArray":
return value;
default:
// no other type should be stored in the bag classes
//log.error ("Unhandled type: " + typeName);
throw new UnsupportedTypeException (type);
}
}
return null;
}
abstract public String toJsonString ();
@Override
public String toString () {
return toJsonString ();
}
@Override
public boolean equals (Object object) {
return toString ().equals (object.toString ());
}
@Override
public int hashCode () {
return toString ().hashCode ();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy