public class PersistorToJSONString extends PersistorToHierarchialString
{ "Response": { "StatusCode": "200", "Headers" : "{Date=Tue, 13 Dec 2016 03:32:13 GMT, Content-Length=4574, Content-Type=application/xml}", "EntityBody" : "<?xml version=\"1.0\"?><CUSTOMERList xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n <CUSTOMER xlink:href=\"http://www.thomas-bayer.com/sqlrest/CUSTOMER/0/\">0</CUSTOMER> \n</CUSTOMERList>", "Request": { "Method": "GET", "Headers": "{Date=Tue, 06 Dec 2016 03:08:12 GMT, Content-Length=4574}" } } }NOTE: Be careful with the value occupying multiple lines, which should be escaped as characters \n; the double quote " should be escaped as \". The example is shown as above.
Modifier and Type | Field and Description |
---|---|
(package private) org.json.JSONObject |
jsonObject |
JSON_ARRAY_BRACKET_LEFT, JSON_ARRAY_BRACKET_RIGHT, JSON_ARRAY_COMMA_SEP
ignoredFieldsForUnpickle, stringFormat, stringFormatReader
Constructor and Description |
---|
PersistorToJSONString() |
PersistorToJSONString(java.io.Reader stringFormatReader) |
PersistorToJSONString(java.lang.String stringFormat) |
Modifier and Type | Method and Description |
---|---|
protected void |
beforeUnpickle()
Prepare the parser with the string format of a Persistable object.
|
protected Persistable |
doUnpickle()
Convert the string format of a Persistable object to Persistable object.
|
protected java.lang.String |
escape(java.lang.String value)
Escape special characters such as value occupying multiple lines, which should be escaped
as characters \n; the double quote should be escaped as
\".
|
protected java.lang.String |
getChildBegin(java.lang.String key,
java.lang.String value,
java.lang.String classname)
This is called inside
PersistorToHierarchialString.parse(Persistable, boolean) to write "key"
and "value" of a child. |
protected java.lang.String |
getChildEnd(boolean lastChild)
This is called inside
PersistorToHierarchialString.parse(Persistable, boolean) to write the
end of a child, here a newline "\n" is written. |
protected java.lang.String |
getContainerBegin(java.lang.String className,
boolean needLeadingName)
This is called inside
PersistorToHierarchialString.parse(Persistable, boolean) to write the begin
of a container such as:
<tagNam> for XML format
"tagName" : {\n for JSON format
|
protected java.lang.String |
getContainerEnd(java.lang.String className)
This is called inside
PersistorToHierarchialString.parse(Persistable, boolean) to write the end
of a container such as:
</tagNam> for XML file
} for JSON file. |
protected boolean |
stringNeedQuoted()
If the string value needs to be quoted before writing into a kind of persistence.
JSON file requires that for string value. In JSON file, For example: "key" : "stringValue" is good "key" : stringValue is bad |
static Persistable |
unpickleParse(org.json.JSONObject body)
Try to convert JSON File to a
Persistable object.In JSON file, the special JSON key Constants.JSONConstants.PROPERTY_CLASSNAME holds the name of the class which the
JSONObject represents; the other keys are the name of the fields of that class. |
protected void |
writeHeader(Persistable persistable)
This is called before
PersistorToString.write(Persistable) . |
protected void |
writeTailer(Persistable persistable)
This is called after
PersistorToString.write(Persistable) . |
getTagName, parse, parseArrayField, parseFieldValue
getPersistenceName, getStringFormat, getStringFormatReader, getType, isIgnoredField, persist, setStringFormat, setStringFormatReader, unpersist, unpickle, write
validate
public PersistorToJSONString()
public PersistorToJSONString(java.lang.String stringFormat)
public PersistorToJSONString(java.io.Reader stringFormatReader)
protected void writeHeader(Persistable persistable) throws SAFSException
PersistorToString
PersistorToString.write(Persistable)
.writeHeader
in class PersistorToString
SAFSException
protected void writeTailer(Persistable persistable) throws SAFSException
PersistorToString
PersistorToString.write(Persistable)
.writeTailer
in class PersistorToString
SAFSException
protected void beforeUnpickle() throws SAFSException
PersistorToString
beforeUnpickle
in class PersistorToString
SAFSException
protected Persistable doUnpickle() throws SAFSException
PersistorToString
doUnpickle
in class PersistorToString
SAFSException
public static Persistable unpickleParse(org.json.JSONObject body) throws SAFSException
Persistable
object.Constants.JSONConstants.PROPERTY_CLASSNAME
holds the name of the class which the
JSONObject represents; the other keys are the name of the fields of that class.body
- JSONObjectSAFSException
protected boolean stringNeedQuoted()
PersistorToString
stringNeedQuoted
in class PersistorToString
protected java.lang.String escape(java.lang.String value)
escape
in class PersistorToString
value
- String, the value to escapePersistorToString.stringNeedQuoted()
protected java.lang.String getContainerBegin(java.lang.String className, boolean needLeadingName)
PersistorToHierarchialString
PersistorToHierarchialString.parse(Persistable, boolean)
to write the begin
of a container such as:
getContainerBegin
in class PersistorToHierarchialString
className
- String, the class name of a container. This is a full class-name, which may needs to
treated to get the simple class name as the tag-name.needLeadingName
- boolean, if the leading tag name is needed. For example sometimes we don't need the "tagName" for JSON format.protected java.lang.String getContainerEnd(java.lang.String className)
PersistorToHierarchialString
PersistorToHierarchialString.parse(Persistable, boolean)
to write the end
of a container such as:
getContainerEnd
in class PersistorToHierarchialString
className
- String, the tag name, it is normally a container. This is a full class-name, which may needs to
treated to get the simple class name as the tag-name.protected java.lang.String getChildBegin(java.lang.String key, java.lang.String value, java.lang.String classname)
PersistorToHierarchialString
PersistorToHierarchialString.parse(Persistable, boolean)
to write "key"
and "value" of a child.getChildBegin
in class PersistorToHierarchialString
key
- String, the key namevalue
- String, the valueclassname
- String, the class name of this value. It is not useful with JSON format, but it is useful for XML format.protected java.lang.String getChildEnd(boolean lastChild)
PersistorToHierarchialString
PersistorToHierarchialString.parse(Persistable, boolean)
to write the
end of a child, here a newline "\n" is written.getChildEnd
in class PersistorToHierarchialString
lastChild
- boolean, if this is the last child within the container.
it is useful for some file format, such as JSON, if this
is false (not the last child), a comma ,
needs to be added at the end as "key" : "value" ,Copyright © SAS Institute. All Rights Reserved.