core.bones.json

Module Contents

Classes

JsonBone

This bone saves its content as a JSON-string, but unpacks its content to a dict or list when used.

class core.bones.json.JsonBone(indexed=False, multiple=False, languages=None, schema={}, *args, **kwargs)

Bases: viur.core.bones.raw.RawBone

This bone saves its content as a JSON-string, but unpacks its content to a dict or list when used. :param schema If provided we can control and verify which data to accept.

# Example
schema= {"type": "object", "properties" :{"price": {"type": "number"},"name": {"type": "string"}}
# This will only accept the provided JSON when price is a number and name is a string.

Initializes a new Bone.

Parameters:
  • indexed (bool) –

  • multiple (bool) –

  • languages (bool) –

  • schema (Mapping) –

type = 'raw.json'
serialize(skel, name, parentIndexed)

Serializes this bone into a format that can be written into the datastore.

Parameters:
  • skel (SkeletonInstance) – A SkeletonInstance object containing the values to be serialized.

  • name (str) – A string representing the property name of the bone in its Skeleton (not the description).

  • parentIndexed (bool) – A boolean indicating whether the parent bone is indexed.

Returns:

A boolean indicating whether the serialization was successful.

Return type:

bool

unserialize(skel, name)

Deserialize bone data from the datastore and populate the bone with the deserialized values.

This function is the inverse of the serialize function. It converts data from the datastore into a format that can be used by the bones in the skeleton.

Parameters:
  • skel (viur.core.skeleton.SkeletonInstance) – A SkeletonInstance object containing the values to be deserialized.

  • name (str) – The property name of the bone in its Skeleton (not the description).

Returns:

True if deserialization is successful, False otherwise.

Return type:

bool

singleValueFromClient(value, skel, bone_name, client_data)

Load a single value from a client

Parameters:
  • value (Union[str, list, dict]) – The single value which should be loaded.

  • skel – The SkeletonInstance where the value should be loaded into.

  • bone_name – The bone name of this bone in the SkeletonInstance.

  • client_data – The data taken from the client, a dictionary with usually bone names as key

Returns:

A tuple. If the value is valid, the first element is the parsed value and the second is None. If the value is invalid or not parseable, the first element is a empty value and the second a list of ReadFromClientError.

structure()

Describes the bone and its settings as an JSON-serializable dict. This function has to be implemented for subsequent, specialized bone types.

Return type:

dict