def test_list_simple_use(self): class Person(Declared): name = var(str) age = var(int) Persons = vec(Person) data = '[{"name": "Tom", "age": 18}, {"name": "Jack", "age": 18}, {"name": "Jerry", "age": 18}]' persons = json.unmarshal(Persons, data) self.assertEqual(json.marshal(persons), data)
class Object(Declared): __xml_tag_name__ = "Object" action = pascalcase_var(str, as_xml_attr=True) code = pascalcase_var(str, as_xml_attr=True) id_ = var(str, field_name="ID", as_xml_attr=True) element_type = pascalcase_var(str, as_xml_attr=True) properties = vec(Property, field_name="Property")
def test_simple_use(self): xml_string = """ <?xml version="1.0" encoding="utf-8"?> <data> <country name="Liechtenstein"> <rank>1</rank> <year>2008</year> <gdppc>141100</gdppc> <neighbor name="Austria" direction="E"/> </country> <country name="Singapore"> <rank>4</rank> <year>2011</year> <gdppc>59900</gdppc> <neighbor name="Malaysia" direction="N"/> </country> <country name="Panama"> <rank>68</rank> <year>2011</year> <gdppc>13600</gdppc> <neighbor name="Costa Rica" direction="W"/> </country> </data> """.strip() class Neighbor(Declared): name = var(str, as_xml_attr=True) direction = var(str, as_xml_attr=True) class Country(Declared): rank = var(str) year = var(int) gdppc = var(int) name = var(str, as_xml_attr=True) neighbor = var(Neighbor) Data = vec(Country, field_name="country") data = xml.unmarshal(Data, ET.XML(xml_string)) self.assertEqual( ET.tostring(xml.marshal(data, xml.Options(True))).decode(), '<data><country name="Liechtenstein"><rank>1</rank><year>2008</year><gdppc>141100</gdppc>' '<neighbor direction="E" name="Austria" /></country><country name="Singapore"><rank>4</rank>' '<year>2011</year><gdppc>59900</gdppc><neighbor direction="N" name="Malaysia" /></country>' '<country name="Panama"><rank>68</rank><year>2011</year><gdppc>13600</gdppc>' '<neighbor direction="W" name="Costa Rica" /></country></data>', ) v = json.Vec(data.vec) v.extend(data) self.assertEqual( json.marshal(v), '[{"rank": "1", "year": 2008, "gdppc": 141100, "name": "Liechtenstein", ' '"neighbor": {"name": "Austria", "direction": "E"}}, {"rank": "4", "year": 2011, ' '"gdppc": 59900, "name": "Singapore", "neighbor": {"name": "Malaysia", "direction": "N"}}, ' '{"rank": "68", "year": 2011, "gdppc": 13600, "name": "Panama", "neighbor": ' '{"name": "Costa Rica", "direction": "W"}}]', )
class Mapping(Declared): __xml_tag_name__ = "Mapping" element_code = pascalcase_var(str, as_xml_attr=True) element_type = pascalcase_var(str, as_xml_attr=True) element_id = var(str, as_xml_attr=True, field_name="ElementID") parent_type = pascalcase_var(str, as_xml_attr=True) parent_code = pascalcase_var(str, as_xml_attr=True) parent_id = var(str, as_xml_attr=True, field_name="ParentID") action = pascalcase_var(str, as_xml_attr=True) properties = vec(Property, field_name="Property")
class Person(Declared): name = var(str) age = var(int) home = vec(Home)
class Mappings(Declared): __xml_tag_name__ = "Mappings" mapping = vec(Mapping, field_name="Mapping")
class Objects(Declared): __xml_tag_name__ = "Objects" object_ = vec(Object, field_name="Object")
class Style(Declared): name = var(str, as_xml_attr=True) parent = var(str, as_xml_attr=True, required=False) items = vec(Item, field_name="item")
def test_other_simple_use(self): xml_string = """ <resources> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <style name="AppTheme.NoActionBar"> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> </style> <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /> <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" /> <style name="ratingBarStyle" parent="@android:style/Widget.RatingBar"> <item name="android:progressDrawable">@drawable/ratingbar_drawable</item> <item name="android:minHeight">48dip</item> <item name="android:maxHeight">48dip</item> </style> </resources> """.strip() class Item(Declared): name = var(str, as_xml_attr=True) text = var(str, as_xml_text=True) class Style(Declared): name = var(str, as_xml_attr=True) parent = var(str, as_xml_attr=True, required=False) items = vec(Item, field_name="item") Resource = vec(Style, field_name="style") data = xml.unmarshal(Resource, ET.XML(xml_string)) assert len(data) == 5 assert format_xml(ET.tostring(xml.marshal(data, xml.Options(True))).decode()) == format_xml(xml_string) v = json.Vec(data.vec) v.extend(data) assert ( json.marshal(v) == '[{"name": "AppTheme", "parent": "Theme.AppCompat.Light.DarkActionBar", "item": ' '[{"name": "colorPrimary", "text": "@color/colorPrimary"}, ' '{"name": "colorPrimaryDark", "text": "@color/colorPrimaryDark"}, ' '{"name": "colorAccent", "text": "@color/colorAccent"}]}, ' '{"name": "AppTheme.NoActionBar", "parent": null, "item": [' '{"name": "windowActionBar", "text": "false"}, {"name": "windowNoTitle", "text": "true"}]}, ' '{"name": "AppTheme.AppBarOverlay", "parent": "ThemeOverlay.AppCompat.Dark.ActionBar", "item": []}, ' '{"name": "AppTheme.PopupOverlay", "parent": "ThemeOverlay.AppCompat.Light", "item": []}, ' '{"name": "ratingBarStyle", "parent": "@android:style/Widget.RatingBar", "item": ' '[{"name": "android:progressDrawable", "text": "@drawable/ratingbar_drawable"}, ' '{"name": "android:minHeight", "text": "48dip"}, ' '{"name": "android:maxHeight", "text": "48dip"}]}]' ) assert ( json.marshal(v, json.Options(True)) == '[{"name": "AppTheme", "parent": "Theme.AppCompat.Light.DarkActionBar", "item": ' '[{"name": "colorPrimary", "text": "@color/colorPrimary"}, ' '{"name": "colorPrimaryDark", "text": "@color/colorPrimaryDark"}, ' '{"name": "colorAccent", "text": "@color/colorAccent"}]}, ' '{"name": "AppTheme.NoActionBar", "item": [{"name": "windowActionBar", "text": "false"}, ' '{"name": "windowNoTitle", "text": "true"}]}, ' '{"name": "AppTheme.AppBarOverlay", "parent": "ThemeOverlay.AppCompat.Dark.ActionBar", "item": []}, ' '{"name": "AppTheme.PopupOverlay", "parent": "ThemeOverlay.AppCompat.Light", "item": []}, ' '{"name": "ratingBarStyle", "parent": "@android:style/Widget.RatingBar", "item": ' '[{"name": "android:progressDrawable", "text": "@drawable/ratingbar_drawable"}, ' '{"name": "android:minHeight", "text": "48dip"}, {"name": "android:maxHeight", "text": "48dip"}]}]' )
class Struct(Declared): p0 = vec(int)
class Product(Declared): pay_types = vec(PayType)
class Test(Declared): picture_navi_ids = vec(int, naming_style=NamingStyle.camelcase)
class Product(Declared): pay_types = vec(int)