Usage¶
Note
For extended and more in depth examples, refer to Examples
The following example illustrates how this module works.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | from json2py.models import *
class Example(NestedField):
hello = TextField(name = 'hi')
integer = IntegerField()
floating = FloatField()
class ExampleList(ListField):
__model__ = Example
dict_var = {'hi': 'world', 'integer': 1000, 'floating': 10.5, 'ignored': "you won't see me"}
list_var = [dict_var] * 3
myMappedList = ExampleList(list_var)
myMappedList[1].integer.value = 1234
print myMappedList.json_encode(indent = 4)
|
Should return something like:
[
{
"integer": 1000,
"floating": 10.5,
"hello": "world"
},
{
"integer": 1234,
"floating": 10.5,
"hello": "world"
},
{
"integer": 1000,
"floating": 10.5,
"hello": "world"
}
]
Models¶
Note
The classes of this modules are intended to be reimplemented in order to make use of this module.
Models represent basic JSON data types. The usage of this models is intended to be subclassed in order to fully map the original JSON structure.
BaseField¶
-
class
json2py.models.
BaseField
(**kwargs)[source]¶ Base Class holding and defining common features for all the other subclasses.
Parameters: name – Name of the field in source data. Note: This class must be treated as abstract class and should not be reimplemented. -
json_decode
(data, **kwargs)[source]¶ Parses a JSON-string into this object. This method is intended to build the JSON to Object map, so it doesn’t return any value, instead, the object is built into self.
Parameters: - data – JSON-string passed to
json.loads()
- kwargs – Parameters passed to
json.loads()
- data – JSON-string passed to
-
json_encode
(**kwargs)[source]¶ Converts an object of class
BaseField
into JSON representation (string) usingBaseEncoder
JSONEncoder.Parameters: kwargs – Parameters passed to json.dumps()
Returns: JSON-string representation of this object.
-
TextField¶
IntegerField¶
FloatField¶
BooleanField¶
-
class
json2py.models.
BooleanField
(*args, **kwargs)[source]¶ Class representing boolean field in JSON.
Parameters: - name – It has the same meaning as in
BaseField
- value – It is the raw data that is this object will represent once parsed.
Raises: ParseException – If
value
is not boolean nor None- name – It has the same meaning as in
Todo
Document how to access elements in NestedField
with same name than Python’s reserved keywords.
NestedField¶
ListField¶
-
class
json2py.models.
ListField
(value=None, *args, **kwargs)[source]¶ Class representing a list field in JSON. This class implements
list
interface so you can slicing, appending, popping, etc.Parameters: - name – It has the same meaning as in
BaseField
- value – It is the raw data that is this object will represent once parsed.
Raises: ParseException – If
value
is not a list nor NoneNote: Hinting the structure of values of the list should be done using the meta variable
__model__
inside class reimplementation.Note: JSON lists’ values can be of any type even in the same list, but in real world apps, every JSON lists’ values should be of the same type, this behaviour also simplifies this module, so this class expects that all values in lists must have the same structure.
- name – It has the same meaning as in