arjuna.tpi.parser.json module

Classes to assist in JSON Parsing.

class arjuna.tpi.parser.json.Json

Bases: object

Helper class to create Arjuna’s Json elements.

classmethod assert_dict_type(obj, *, msg)

Assert that the provided object is a Python dict or JsonDict object.

Keyword Arguments

msg – Purpose of this assertion.

classmethod assert_list_type(obj, *, msg)

Assert that the provided object is a Python list or JsonList object.

Keyword Arguments

msg – Purpose of this assertion.

classmethod extract_schema(jobj_or_str) arjuna.tpi.parser.json.JsonSchema

Extracts schema from provide Json object or string to create a JsonSchema object.

Parameters

jobj_or_str – JsonDict/JsonList or a Python list/dict or a string representing a Python object

classmethod from_file(file_path: str, *, allow_any: bool = False) JsonDictOrList

Creates a Json object from file.

Parameters

file_path – Absolute path of the json file.

Keyword Arguments

allow_any – If True, if the object can not be coverted to a JsonDict/JsonList, same object is returned, else an Exception is raised.

Returns

Arjuna’s JsonDict or JsonList object or the same object for allow_any = True

classmethod from_iter(iter) arjuna.tpi.parser.json.JsonList

Creates a Json object from Python iterator.

Parameters

iter – Python iterable

Returns

Arjuna’s JsonList object

classmethod from_map(map: dict) arjuna.tpi.parser.json.JsonDict

Creates a JsonDict object from Python dictionary.

Parameters

map – Python dict

Returns

Arjuna’s JsonDict object

classmethod from_object(jobj, *, allow_any=False)

Convert a Python object to a JsonDict/JsonList object if applicable.

Any iterable which is not a dict is converted to a JsonList.

Parameters

jobj – dict/list/tuple/set or a compatible Python object.

Keyword Arguments

allow_any – If True, if the object can not be coverted, same object is returned, else an Exception is raised.

classmethod from_str(jstr: str, *, allow_any: bool = False) JsonDictOrList

Creates a Json object from a JSON string.

Parameters

jstr – A string representing a compatible Python object.

Keyword Arguments

allow_any – If True, if the object can not be coverted to a JsonDict/JsonList, same object is returned, else an Exception is raised.

Returns

Arjuna’s JsonDict or JsonList object or the same object for allow_any = True

classmethod reset_auto_id_key()

Set Auto ID Key for jsonpath lib to None. Has global impact.

classmethod set_auto_id_key(key='id')

Set Auto ID Key for jsonpath lib to provided key.

Parameters

key – Name of key. Default is id

class arjuna.tpi.parser.json.JsonDict(pydict: dict = None)

Bases: arjuna.tpi.helper.arjtype._ArDict, arjuna.tpi.parser.json.JsonElement

Encapsulates Dictionary object in Json.

Parameters

pydict – Python dict.

Note

Supports dictionary methods as well as . access for key.

Also supports == operator. Right operand can be a Python dict or a JsonDict or a YamlDict or any custom DataEntity object.

assert_contents(*, msg, **key_values)

Assert that the values of provided keys match in the root of this dict.

Parameters
  • msg – Purpose of this assertion.

  • **key_values – Arbitrary key-value pairs to match in the root of this dict.

assert_keys_present(*keys, msg)

Assert that the provided keys are present in the root of this dict.

Parameters
  • msg – Purpose of this assertion.

  • **keys – Key names to assert

assert_match(jobj, *, msg, ignore=None)

Assert that this JsonDict matches the provided object.

Parameters

jobj – Python dict/JsonDict

Keyword Arguments
  • msg – Purpose of this assertion.

  • ignore – jpath to be ignored while matching

assert_match_schema(jobj, *, msg)

Assert that this JsonDict matches the schema of provided object.

Parameters

jobj – Json string or Python dict/JsonDict

Keyword Arguments

msg – Purpose of this assertion.

assert_schema(schema, *, msg)

Assert that this JsonDict matches the provided schema.

Parameters

schema – Python dict schema or JsonSchema object.

Keyword Arguments

msg – Purpose of this assertion.

matches_schema(schema)

Check if this JsonDict matches the provided schema

Parameters

schema – Python dict schema or JsonSchema object.

Returns

A 2-element tuple (True/False, List of error strings in matching)

property raw_object

A copy of the Python dict that this object wraps.

property schema: arjuna.tpi.parser.json.JsonSchema

Schema of this object.

property size

Number of key-value pairs in this object.

class arjuna.tpi.parser.json.JsonElement(container)

Bases: arjuna.tpi.engine.asserter.AsserterMixIn, arjuna.tpi.engine.asserter.IterableAsserterMixin

Abstract Json Element. Base class for JsonList and JsonDict.

find(query) Any

Find first element with JsonPath.

Parameters

query – JsonPath Query string

Returns

A Json element.

findall(query) list

Find all elements with JsonPath query.

Parameters

query – JsonPath Query string

Returns

python list of found Json elements.

class arjuna.tpi.parser.json.JsonList(pylist: list)

Bases: arjuna.tpi.parser.json.JsonElement

Encapsulates a list object in Json.

Parameters

pylist – Python list

Note

Supports indexing just like a Python list.

Also supports == operator. Right operand can be a Python list or a JsonList or a YamlList object.

append(object)

Append object to the end of the JsonList.

extend(iterable)

Extend JsonList by appending elements from the iterable.

pop(index=- 1)

Remove and return item at index (default last).

property raw_object

A copy of the Python list that this object wraps.

property size

Number of objects in this list.

class arjuna.tpi.parser.json.JsonSchema(schema_dict)

Bases: object

Encapsulates Json Schema which can be used to validate Json objects.

allow_null(*keys)

Allow None as a value type for the provided keys in the root of Json Schema.

Parameters

*keys – Arbitrary key names

as_dict() dict

Get this object as a Python dict.

classmethod builder(*, schema_uri=None)

Create a JsonSchemaBuilder.

Keyword Arguments

schema_uri – URI to be set for ‘$schema’ key.

mark_optional(*keys)

Mark the provided keys as optional in the root of Json Schema.

Parameters

*keys – Arbitrary key names

set_allowed_values(*values)

Set allowed values.

Parameters

*values – Arbitrary allowed values.

set_maximum(number)

Set maximum occurances.

Parameters

number – Upper limit for occurances.

class arjuna.tpi.parser.json.JsonSchemaBuilder(*, schema_uri=None)

Bases: object

JsonSchema builder class.

Keyword Arguments

schema_uri – URI to be set for ‘$schema’ key.

build()

Create JsonSchema based on the building constructs provided till this point.

object(jobj)

Add schema based on an object to the overall schema.

Call multiple times to add more objects.

Parameters

jobj_or_str – JsonDict/JsonList or a Python str/list/dict.

schema(schema)

Add schema to the overall schema.

Call multiple times to add more schema.

Parameters

schema – Json schema dict

string(jstr)

Add schema based on an object string to the overall schema.

Call multiple times to add more objects.

Parameters

jstr – A string representing a Python object.