Search 1.3 billion lines of Odoo code on GitHub

base_jsonify

Author: Akretion, Odoo Community Association (OCA)
License: AGPL-3
Branch: 14.0
Repository: acsone/server-tools
Dependencies: base
Languages: HTML (460, 33.0%), PO File (105, 7.5%), Python (622, 44.7%), XML (83, 6.0%), and reStructuredText (123, 8.8%)
Other branches: 10.0, 10.0-abi_master, 10.0-backport-base_m2m_custom_field, 10.0-backport-slow_statement_logger-lmi, 10.0-backport_12.0-lmi, 10.0-fix-base_exception-dro, 10.0-fix-base_techical_features_cache-dro, 10.0-ftd_master, 10.0-imp-mass_editing-groups-dro, 10.0-imp-mass_editing_hooks-dro, 10.0-imp-task-13269, 10.0-jsonify-cpi, 10.0-lns_master, 10.0-mass_editing_improvements-dro, 10.0-ref_keychain_account_index_tbi, 12.0, 12.0-asw_master, 12.0-backport-shopinvader_customer_multi_user-len, 12.0-bxp_master, 12.0-jsonifyfull-len, 12.0-jsonifystored-len, 12.0-jsonifystoredsimple-len, 12.0-partition-len, 12.0-trn_master, 12.0-xmlify-len, 13.0, 13.0-MIG-base_jsonify, 13.0-custom_sequence_code, 13.0-jsonifyfull-len, 13.0-jsonifystored-len, 13.0-jsonifystorednew-len, 13.0-mig_auto_export, 13.0-partition-len, 13.0-rlf_master, 13.0-slg_master, 13.0-xmlify-len, 9.0-backport-base-jsonify, 9.0-slg_master, and bai_10.0_add_base_ir_values_groups
Other repositories: 001101/server-tools, 0yik/ak-odoo-incubator, 0yik/server-tools, 2FM/server-tools, 50611/server-tools, AITIC/server-tools, AMhadhbi/server-tools, Albin-John/server-tools, Alex-Woo/server-tools, Alfa-90/server-tools, Artielkami/server-tools, CB-Gran-Canaria/server-tools, CGenie/server-tools, Cgruppo/server-tools, Change2improve/server-tools, ChinaShrimp/server-tools, CodeFishEG/server-tools, Colorlessgold/server-tools, ComitorDevTeam/server-tools, Comunitea/server-tools, ContrerasTorrez/server-tools, CrazyJET/server-tools, DITIntl/server-tools, Danielzhulin/server-tools, Digital5-Odoo/server-tools, Dobtor-OCA/server-tools, DucTruongKomit/server-tools, DulingLai/server-tools, EBII/ak-odoo-incubator, ERPLibre/server-tools, FernandoRomera/server-tools, GSLabIt/server-tools, Gabinete-Digital/server-tools, Gebesa-Dev/server-tools, GleydsonHudson/server-tools, Glue-Software-Engineering-AG/server-tools, HomerDeoDatu/server-tools, Ingeos/server-tools, Intesco2-0/server-tools, IvanShirokikh/server-tools, Jarsa/server-tools, Jeisonpernia/server-tools, JuaniFreedoo/server-tools, JuniorKRnd/server-tools, Koesj/server-tools, KonaERP/server-tools, KottmannM/server-tools, LasLabs/server-tools, LevelPrime/server-tools, LlywelynLi/server-tools, LourenzoH/server-tools, Mahmoud-Alaa-Eldeen/server-tools, MediaJockey/server-tools, Mefistous/server-tools, Melodivo/server-tools, MindAndGo/server-tools, MjAbuz/server-tools, NGTS-AUS/server-tools, OCA-MUK/server-tools, OCA/server-tools, OdooBulgaria/ak-odoo-incubator, OdooIndonesia/server-tools, OpenAT/ak-odoo-incubator, Othniel1796/server-tools, PieterPaulussen/server-tools, PlanetaTIC/server-tools, Ppamungkas/server-tools, PrenorSistemas/server-tools, QubiQ/server-tools, RL-OtherApps/ak-odoo-incubator, Rad0van/server-tools, Reinhard-sheng/server-tools, SanteLibre/server-tools, ScopeaFrance/server-tools, SequarSrl/server-tools, SeuMarco/server-tools, SilvioGregorini/server-tools, SimoneVagile/server-tools, SokreamPhanqrl/server-tools, Syci-Consulting/server-tools, TRESCLOUD/server-tools, Tecnativa/server-tools, Thorncrag/server-tools, TimLai125/server-tools, Tonow-c2c/server-tools, UnknownInfo/server-tools, VanMoof/server-tools, Yaman-als/server-tools, YogeshMahera-SerpentCS/server-tools, aadarsh977/server-tools, aaltinisik/server-tools, abakus-it/server-tools, abdulhalim-cu/server-tools, abdulrhmans/server-tools, acsone/ak-odoo-incubator, adekock11/server-tools, aitorbouzas/server-tools, ajayjaviya/server-tools, ajinvn2019/server-tools, akretion/ak-odoo-incubator, akretion/server-tools, alan80080/server-tools, alessavindell/server-tools, alfadil/server-tools, altoch/server-tools, amendoncabh/server-tools, anassahmed/server-tools, andriisem/server-tools, anhvu-sg/server-tools, anodino-dev/server-tools, ansinaesmejor/server-tools, antilhue/server-tools, area-scm/server-tools, aurestic/server-tools, azmeuk/server-tools, bacgdev/server-tools, bacgroup/server-tools, bala07123/server-tools, bangil0/server-tools, bealdav/server-tools, bicomm/server-tools, bmya/server-tools, bodi000/server-tools, boky1993/server-tools, boutitinizar/server-tools, brain-tec/server-tools, brainbeanapps/server-tools, camptocamp/ak-odoo-incubator, camptocamp/server-tools, captivea-dcr/server-tools, cheikhousy/server-tools, claro5/server-tools, coopiteasy/server-tools, corsojulian7/server-tools, crnd-inc/server-tools, dalal-intellisoft/server-tools, damdam-s/server-tools, dani-xtendoo/server-tools, daramousk/server-tools, davidwah/server-tools, delgerS/server-tools, dennybiasiolli/server-tools, devang-dreambits/server-tools, dipakthapaofficial/server-tools, dmsergio/server-tools, dndoanh/server-tools, dong-z/server-tools, druidoo-dev/server-tools, dsasoftware/ak-odoo-incubator, ebirbe/server-tools, ecosoft-odoo/server-tools, eezee-it/server-tools, elicoidal/server-tools, emagdalenaC2i/server-tools, erfanjahanshahlo/server-tools, ermannognan/server-tools, etobella/server-tools, eversatis/server-tools, ewac-nl/server-tools, exosoftware/oca-server-tools, ferdiansahgg/server-tools, florian-dacosta/ak-odoo-incubator, fmdl/server-tools, focusate/oca-server-tools, gab-net/server-tools, gitguohao/server-tools, goanzaloo/server-tools, grindtildeath/server-tools, guandalf/server-tools, guewen/server-tools, gurneyalex/server-tools, habbaba/server-tools, hadjerBachiri/ak-odoo-incubator, hbrunn/server-tools, hdb1234/server-tools, henrryyanez/server-tools, hibou-io/oca-server-tools, hiisoc/server-tools, hinfo506/server-tools, hparfr/server-tools, hq8399/server-tools, hvcabaim/server-tools, i-vyshnevska/server-tools, iamyeonhan/ak-odoo-incubator, ideaspositivas-group/server-tools, igallart/server-tools, igor-mitin/server-tools, initOS/server-tools, insscn/server-tools, intero-chz/server-tools, invitu/server-tools, ip-candres/server-tools, izzihector/server-tools, jaballate/server-tools, jackxusho/server-tools, jacky82669513/server-tools, janverb/server-tools, jcarlosmontoya/server-tools, jeanpaul-mupagasi/server-tools, jeroen7s/server-tools, josueBulle/server-tools, josuf567/server-tools, julianram/server-tools, kait-avalah/server-tools, kevinkhao/ak-odoo-incubator, kikopeiro/server-tools, kittiu/server-tools, kmee/server-tools, kos94ok-3D/server-tools, leemannd/server-tools, legalsylvain/server-tools, lembregtse/server-tools, lideritjnma/server-tools, liehu1/server-tools, lilotter45/server-tools, liweijie0812/server-tools, loftwah/server-tools, magnuscolors/ak-odoo-incubator, magnuscolors/server-tools, maherjaballi/server-tools, marcelomora/server-tools, marionumza/ak-odoo-incubator, merchise-autrement/server-tools, mikolodz/server-tools, msdeek/server-tools, mulaudzicalvin/server-tools, multidadosti-erp/server-tools, myonnet/oca-server-tools, nehaksolves/server-tools, newtratip/server-tools, nicolasjelincic/server-tools, nilshamerlinck/server-tools, niulinlnc/server-tools, njeudy/server-tools, ntsirintanis/server-tools, odof/server-tools, odooerpdevelopers/server-tools, odoogap/server-tools, omalbastin/server-tools, onesteinbv/server-tools, onurugur/server-tools, osiell/server-tools, percypalomino/server-tools, phillipstack/server-tools, piensom/server-tools, praxigento/oca-server-tools, presenceverte/server-tools, programadorzurita/server-tools, pythoner-3k/server-tools, raizs/server-tools, rifki/server-tools, rm-jamotion/server-tools, robertsvx27/server-tools, romendil/server-tools, rrgaona/server-tools, rsullivan2704/server-tools, rwfnf/server-tools, sanube/server-tools, sc4you/ak-odoo-incubator, schout-it/server-tools, sebalix/server-tools, shepilov-vladislav/server-tools, simahawk/server-tools, simone/server-tools, skukered/server-tools, smile-888/server-tools, solvosci/server-tools, ssaid/server-tools, steingabelgaard/server-tools, sunflowerit/server-tools, sunshineLhj/server-tools, suprielfath/server-tools, sylvainvh/server-tools, sysprocesarcastro/server-tools, takinobori/oca-server-tools, tarteo/server-tools, tegin/server-tools, tirma-sa/server-tools, tmasson/server-tools, tongjiou/server-tools, tonygalmiche/server-tools, trevi-software/server-tools, tuanna7593/server-tools, unitek-solusi/OCA-server-tools, valenciaricardos/ak-odoo-incubator, valentinthirion/server-tools, valmarnet/server-tools, vdedyukhin/server-tools, ventor-tech/server-tools, versada/ak-odoo-incubator, versada/server-tools, victoralmau/server-tools, vidtsin/ak-odoo-incubator, vmelnych/server-tools, westlyou/ak-odoo-incubator, willmakenoise/server-tools, xAlphaOmega/server-tools, xcgd/server-tools, xichun/server-tools, xtjie/server-tools, xuanmu/server-tools, xwh123807/server-tools, xwh1238070/server-tools, yalabsio/server-tools, ycp1015/server-tools, yelizariev/server-tools, ygdmxy/server-tools, ypapouin/server-tools, yuzhengxing/server-tools, and zhaohengzeng/server-tools

Base Jsonify

Beta License: AGPL-3 OCA/server-tools Translate me on Weblate Try me on Runbot

This module adds a 'jsonify' method to every model of the ORM. It works on the current recordset and requires a single argument 'parser' that specify the field to extract.

Example of a simple parser:

parser = [
    'name',
    'number',
    'create_date',
    ('partner_id', ['id', 'display_name', 'ref'])
    ('line_id', ['id', ('product_id', ['name']), 'price_unit'])
]

In order to be consistent with the odoo api the jsonify method always return a list of object even if there is only one element in the recordset.

By default the key into the json is the name of the field extracted from the model. If you need to specify an alternate name to use as key, you can define your mapping as follow into the parser definition:

parser = [
    'field_name:json_key'
]
parser = [
    'name',
    'number',
    'create_date:creationDate',
    ('partner_id:partners', ['id', 'display_name', 'ref'])
    ('line_id:lines', ['id', ('product_id', ['name']), 'price_unit'])
]

If you need to parse the value of a field in a custom way, you can pass a callable or the name of a method on the model:

parser = [
    ('name', "jsonify_name")  # method name
    ('number', lambda rec, field_name: rec[field_name] * 2))  # callable
]

Also the module provide a method "get_json_parser" on the ir.exports object that generate a parser from an ir.exports configuration.

Further features are available for advanced uses. It defines a simple "resolver" model that has a "python_code" field and a resolve function so that arbitrary functions can be configured to transform fields, or process the resulting dictionary. It is also to specify a lang to extract the translation of any given field.

To use these features, a full parser follows the following structure:

parser = {
    "resolver": 3,
    "language_agnostic": True,
    "langs": {
        False: [
            {'name': 'description'},
            {'name': 'number', 'resolver': 5},
            ({'name': 'partner_id', 'target': 'partner'}, [{'name': 'display_name'}])
        ],
        'fr_FR': [
            {'name': 'description', 'target': 'descriptions_fr'},
            ({'name': 'partner_id', 'target': 'partner'}, [{'name': 'description', 'target': 'description_fr'}])
        ],
    }
}

One would get the a result having this structure (note that the translated fields are merged in the same dictionary):

exported_json == {
    "description": "English description",
    "description_fr": "French description, voilà",
    "number": 42,
    "partner": {
        "display_name": "partner name",
        "description_fr": "French description of that partner",
    },
}

Note that a resolver can be passed either as a recordset or as an id, so as to be fully serializable. A slightly simpler version in case the translation of fields is not needed, but other features like custom resolvers are:

parser = {
    "resolver": 3,
    "fields": [
            {'name': 'description'},
            {'name': 'number', 'resolver': 5},
            ({'name': 'partner_id', 'target': 'partners'}, [{'name': 'display_name'}]),
    ],
}

By passing the fields key instead of langs, we have essentially the same behaviour as simple parsers, with the added benefit of being able to use resolvers.

Standard use-cases of resolvers are: - give field-specific defaults (e.g. "" instead of None) - cast a field type (e.g. int()) - alias a particular field for a specific export - ...

A simple parser is simply translated into a full parser at export.

If the global resolver is given, then the json_dict goes through:

resolver.resolve(dict, record)

Which allows to add external data from the context or transform the dictionary if necessary. Similarly if given for a field the resolver evaluates the result.

It is possible for a target to have a marshaller by ending the target with '=list': in that case the result is put into a list.

parser = {
    fields: [
        {'name': 'name'},
        {'name': 'field_1', 'target': 'customTags=list'},
        {'name': 'field_2', 'target': 'customTags=list'},
    ]
}

Would result in the following json structure:

{
    'name': 'record_name',
    'customTags': ['field_1_value', 'field_2_value'],
}

The intended use-case is to be compatible with APIs that require all translated parameters to be exported simultaneously, and ask for custom properties to be put in a sub-dictionary. Since it is often the case that some of these requirements are optional, new requirements could be met without needing to add field or change any code.

Note that the export values with the simple parser depends on the record's lang; this is in contrast with full parsers which are designed to be language agnostic.

Table of contents

Bug Tracker

Bugs are tracked on GitHub Issues. In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us smashing it by providing a detailed and welcomed feedback.

Do not contact contributors directly about support or help with technical issues.

Credits

Authors

  • Akretion

Contributors

Maintainers

This module is maintained by the OCA.

Odoo Community Association

OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use.

This module is part of the OCA/server-tools project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.