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
(value=None, name=None, required=True)[source]¶ Base Class holding and defining common features for all the other subclasses.
Parameters: - value – Value to be stored
- name – Name of the field in source data.
- required – Whether raise LookupError when key is missing or not.
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 itself.
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¶
NestedField¶
ListField¶
-
class
json2py.models.
ListField
(value=None, name=None, required=True)[source]¶ Class representing a list field in JSON. This class implements
list
interface so you can slicing, appending, popping, etc.Parameters: 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.
DateField¶
-
class
json2py.models.
DateField
(value=None, name=None, required=True, formatting='%Y-%m-%dT%H:%M:%SZ')[source]¶ Class used to parse and represent dates. It makes use of
datetime
anddateutil
.Parameters: Raises: ParseException – If
value
is not valid nor NoneNote: Several format’s can be in
formatting
: auto: usedateutil.parser.parse()
, timestamp: provide UNIX timestamp and usedatetime.datetime.utcfromtimestamp()
and custom string: use any format in compliance withdatetime.datetime.strptime()
valid formats.