the JSON object attached to the parser
Whether the matching of the path to the JSON object is case sensitive. Does not influence the matching of placeholder to a placeholder object.
Returns a value out of the JSON object based on the provided path.
PATH:
The path represents the way down along the nested objects inside the JSON object:
"firstLevelObjectName.secondLevelObjectName.thirdLevelObjectName.valueObjectName"
You can skip levels until a matching object is found:
"firstLevelObjectName..matchingObjectName.firstObjectNameAfterMatch.valueObjectName"
".." indicates to look for the next object name not just in the next level but in the following levels as well. It keeps on parsing the path from there.
"..." works like ".." with the difference when the matching rest path results in no value, it keeps looking for the matchingObjectName inside the matching rest path and starts parsing from a possible second match along the same branch again and so on. ".." stops looking deeper into the branch as soon as it finds a match. Though it keeps on looking in the other branches if the value resolves to nothing/no value (undefined, null, false, "").
You can restrict the deep search by defining a wall which ".." and "..." can not pass. Add "][" behind ".." or "..." where you put objectNames divided by "|" between the brackets which represent the wall. The deep search will not pass these objects. Behind the wall you keep defining your path starting with a ".":
"firstLevelObjectName...]wallObjectName1|wallObjectName2[.valueObjectName"
After the wall an objectName has to follow. Another deep search identifier (".." or "...") is not permitted.
If a given object name represents an array, you have to mark the name as array by ending it with "[]". Inside the brackets you can specify which array item to follow by passing the index. If no index is given, the first item is followed ("[]" is the same as "[0]"):
"firstLevelObjectName..arrayObjectFollowThirdItem[2]..valueObjectName"
"[]" or "[0]" also work on objects which are not arrays:
".notAnArray[].valueObjectName"
If you put "" in front or at the end of an object name means that the name don't has to match exactly
but only the end or the beginning of the name respectively. If you put "" at both ends, the object name
only has to contain the name.
"endOfObjectName...beginningOfObjectName.containsName"
PLACEHOLDER:
You can define placeholder in the path which would be replaced with the corresponding value out of the provided placeholder object. Placeholder are placed in {}:
"..matchingObjectName.{placeholder}.valueObjectName"
Placeholder are string replacements therefore placeholder can replace anything in your path and there values need to be strings except you place a placeholder right at the beginning of your path. In this case the placeholder value needs to be a JSON object because with this way you provide an alternative JSON object to parse:
"{alternativeJSONObject}.firstLevelObjectName.secondLevelObjectName.valueObjectName"
If you want to place an actual placeholder at the beginning of your path, put "." before it:
".{placeholder}.secondLevelObjectName.valueObjectName"
You can also provide a function as placeholder value. The function needs to return eather a string or a JSON object, depending on the position of the placeholder. No return value equals an empty string. The function gets the placeholder object itself as parameter.
If the function is part of a set, the function is called in the scope of the set. You can reference the set with "this" and add or remove key/value pairs to create dynamic sets which react to the values of their resolved path. (Does not work with arrow functions since arrow functions have no own binding to "this".) You could also manipulate the placeholder object but the placeholder object is only used for the current path. In a set the placeholder object is created fresh for each entry in the set.
It is possible to provide multiple placeholder for the same position. If one placeholder value results in nothing (undefined, null, false, ""), the next in line is used. Divide the placeholder with "|":
".firstLevelObjectName.{placeholder1|placeholder2|placeholder3}.valueObjectName"
getValue() returns only a value if the path could be fully parsed. Values which result in nothing (undefined, null, false, "") are considered as none results and let the parser keep on searching. This also means that "false" values will let the parser keep on searching and try to resolve the path into a "true" value but if this is not possible, results in null.
the path of the value
Optional
placeholder: anyan object of placeholder
Resolves a set of path. The set itself is passed as placeholder object for each path.
Since the set itself is passed as placeholder object for each path, values of already resolved path can be used as placeholder for path which come later in the set.
Only entries in the set which are of type String and start with "path:" are actually resolved. Other entries are ignored and can be used to define placeholder.
const set = JSONParser.createSet();
set.add('path1', 'path:firstLevelObjectName.secondLevelObjectName.{placeholder}.valueObjectName');
set.add('path2', 'path:firstLevelObjectName.{path1}.valueObjectName');
set.add('placeholder', 'thirdLevelObjectName');
const values = resolveSet(set);
The function returns an object with the resolved key/value pairs of the set. The set itself is also altered and will contain the resolved values. It can be used to build new sets based on the resolved values.
a set of path to be resolved
an object with key/value pairs of the resolved set
Static
addAdds a path or set to the JSONParser library.
In case of a set it actually stores a copy of the given set.
Use it to build a library of path and sets which can be used later on to parse your JSON objects.
the name under which the path or set gets stored
the path or set to be stored
Static
addAdds a connection between a GraphQL function and a JSONParser set which will be used by the hylo-apollo-client call function to parse the returned data.
If the GraphQL function has an alias defined for the call, the alias is used for comparison. Set the connection by using the alias instead of the actual GraphQl function.
You can add additional key/value pairs which will be added to the JSONParser set before parsing the returned data.
the GraphQl function or its alias (e.g. "allCities")
the JSONParser set to be used to parse the returned data by the hylo-apollo-client call function
Optional
additional_parser_values: { an object of additional key/value pairs to add to the JSONParser set before parsing
Static
createCreates a set.
To add values to the set, use the internal function "add(key, value)" of the set:
const newset = JSONParser.createSet();
newset.add('city', 'path:..city.translatedName');
To overwrite a value, add the value with the same key.
You can also add another set. The key/value pairs of the additional set will be added to the new set:
newset.add(JSONParser.get('additional_set'));
With "remove(key)" you can remove a key/value pair from the set.
To read the value of a key/value pair, convert the set into an object and get the value from there.
Static
getReturns a path or set stored in the library.
In case of a set it returns a copy of the original set.
If you provide a prefix and the returned value is a set, all keys of the set will be prefixed with this value.
If a json value is given, it will be placed in front of each path. It is meant to provide an alternative json object out of a placeholder object. An already defined json object in a path will be replaced. Provide the json value including curly brackets. Since it is placed in front of each path and removes an already defined json object, it can be used to add an initial path as well (or both).
the name of the path or set as defined with add()
Optional
prefix: stringa prefix placed in front of each set key
Optional
json: stringa string placed in front of each path
string|object[]
Static
getReturns the JSONParser set and its additional key/value pairs previously saved by addGraphQLToParser().
Static
removeStatic
setGenerated using TypeDoc
import JSONParser from '@src/hylo-components/utils/jsonparser/jsonparser';
Helper class to parse a JSON object for a specific value or set of values.
First read the descriptions of getValue() and resolveSet() to understand the functionality of the JSONParser.