public class JSONUtils extends Object
Modifier and Type | Field and Description |
---|---|
static String |
ARRAY |
static String |
OBJECT |
Constructor and Description |
---|
JSONUtils() |
Modifier and Type | Method and Description |
---|---|
static BRefValueArray |
convertArrayToJSON(BBooleanArray booleanArray)
Convert
BBooleanArray to JSON. |
static BRefValueArray |
convertArrayToJSON(BFloatArray floatArray)
Convert
BFloatArray to JSON. |
static BRefValueArray |
convertArrayToJSON(BIntArray intArray)
Convert
BIntArray to JSON. |
static BRefValueArray |
convertArrayToJSON(BNewArray bArray)
Convert
BNewArray to JSON. |
static BRefValueArray |
convertArrayToJSON(BStringArray stringArray)
Convert
BStringArray to JSON. |
static BRefType<?> |
convertJSON(BRefType<?> jsonValue,
BType targetType) |
static BMap<String,BValue> |
convertJSONToStruct(BValue json,
BStructureType structType)
Convert a BJSON to a user defined struct.
|
static BRefType<?> |
convertMapToJSON(BMap<String,BValue> map,
BJSONType targetType)
Convert map value to JSON.
|
static BRefValueArray |
convertRefArrayToJSON(BRefValueArray refValueArray)
Convert
BRefValueArray to JSON. |
static BXML |
convertToXML(BValue json,
String attributePrefix,
String arrayEntryTag)
Converts given JSON object to the corresponding XML.
|
static BRefType<?> |
convertUnionTypeToJSON(BRefType<?> source,
BJSONType targetType) |
static BRefType<?> |
getArrayElement(BRefType<?> json,
long index)
Get an element from a JSON array.
|
static BRefType<?> |
getElement(BValue json,
String elementName)
Get an element from a JSON.
|
static BStringArray |
getKeys(BValue json)
Returns the keys of a JSON as a
BStringArray . |
static String |
getTypeName(BValue jsonValue) |
static boolean |
hasElement(BRefType<?> json,
String elementName)
Check whether JSON has particular field.
|
static boolean |
isJSONArray(BValue json)
Check whether provided JSON object is a JSON Array.
|
static boolean |
isJSONObject(BValue json)
Check whether provided JSON object is a JSON Object.
|
static BMap<String,?> |
jsonToBMap(BValue json,
BMapType mapType)
Convert a JSON node to a map.
|
static void |
remove(BValue json,
String fieldName)
Remove a field from JSON.
|
static void |
setArrayElement(BValue json,
long index,
BRefType<?> element)
Set an element in the given position of a JSON array.
|
static void |
setElement(BValue json,
String elementName,
BValue element)
Set an element in a JSON.
|
static BRefType<?> |
toJSON(BTable table,
boolean isInTransaction)
Convert
BTable to JSON. |
public static final String OBJECT
public static final String ARRAY
public static boolean hasElement(BRefType<?> json, String elementName)
json
- JSON to be considered.elementName
- String name json field to be considered.public static BRefValueArray convertArrayToJSON(BIntArray intArray)
BIntArray
to JSON.intArray
- BIntArray
to be converted to JSONpublic static BRefValueArray convertArrayToJSON(BFloatArray floatArray)
BFloatArray
to JSON.floatArray
- BFloatArray
to be converted to JSONpublic static BRefValueArray convertArrayToJSON(BStringArray stringArray)
BStringArray
to JSON.stringArray
- BStringArray
to be converted to JSONpublic static BRefValueArray convertArrayToJSON(BBooleanArray booleanArray)
BBooleanArray
to JSON.booleanArray
- BBooleanArray
to be converted to JSONpublic static BRefValueArray convertArrayToJSON(BNewArray bArray)
BNewArray
to JSON.bArray
- BNewArray
to be converted to JSONpublic static BRefValueArray convertRefArrayToJSON(BRefValueArray refValueArray)
BRefValueArray
to JSON.refValueArray
- BRefValueArray
to be converted to JSONpublic static BRefType<?> convertMapToJSON(BMap<String,BValue> map, BJSONType targetType)
map
- value BMap
to be converted to JSONtargetType
- the target JSON type to be convert topublic static BRefType<?> toJSON(BTable table, boolean isInTransaction)
BTable
to JSON.table
- BTable
to be converted to BStreamingJSON
isInTransaction
- Within a transaction or notpublic static BRefType<?> getElement(BValue json, String elementName)
json
- JSON object to get the element fromelementName
- Name of the element to be retrievedpublic static void setElement(BValue json, String elementName, BValue element)
json
- JSON object to set the elementelementName
- Name of the element to be setelement
- JSON elementpublic static boolean isJSONArray(BValue json)
json
- JSON to execute array condition.public static boolean isJSONObject(BValue json)
json
- JSON to execute array condition.public static BRefType<?> getArrayElement(BRefType<?> json, long index)
json
- JSON array to get the element fromindex
- Index of the element neededpublic static void setArrayElement(BValue json, long index, BRefType<?> element)
json
- JSON array to set the elementindex
- Index of the element to be setelement
- Element to be setpublic static BXML convertToXML(BValue json, String attributePrefix, String arrayEntryTag)
json
- JSON object to get the corresponding XMLattributePrefix
- String prefix used for attributesarrayEntryTag
- String used as the tag in the arrayspublic static BMap<String,?> jsonToBMap(BValue json, BMapType mapType)
json
- JSON to convertmapType
- MapType which the JSON is converted to.BMap
containing the values
of the JSON object. Otherwise a BallerinaException
will be thrown.public static BMap<String,BValue> convertJSONToStruct(BValue json, BStructureType structType)
json
- JSON to convertstructType
- Type (definition) of the target structBMap
containing the values
of the JSON object. Otherwise the method will throw a BallerinaException
.public static BStringArray getKeys(BValue json)
BStringArray
.json
- JSON to get the keysBStringArray
public static BRefType<?> convertUnionTypeToJSON(BRefType<?> source, BJSONType targetType)
public static void remove(BValue json, String fieldName)
json
- JSON objectfieldName
- Name of the field to removeCopyright © 2018 WSO2. All rights reserved.