Usina BRAdvPL Guide
Sign in
← All topics
PRACTICAL GUIDEUnder review

JSON serialization and deserialization

Parses JSON text into an object and serializes it back.

JSON serialization and deserializationAdvPL
01 · OVERVIEW

Overview

FromJson populates a JsonObject and returns Nil on success or error text on failure; its return is not the populated object. Read properties only after checking that result. ToJson produces text whose property order may differ from the input. Valid syntax does not establish a valid business payload: check fields, types, sizes and authorization. Dates need an explicit contract format independent of Protheus display settings. The example uses fictional data and does not log external input. (Sources: refs. 1–3.)

03 · PRACTICAL EXAMPLE

Commented example

#Include "Protheus.ch"

User Function Ed647()
    Local oData := JsonObject():New()
    Local xError := oData:FromJson('{"id":"ED647","active":true}')
    Local cJson := ""
    // Nil means success; text means a parse failure.
    If ValType(xError) == "U"
        cJson := oData:ToJson()
        ConOut(cJson)
    Else
        ConOut("INVALID_JSON")
    EndIf
    FreeObj(oData)
Return Nil
Expected result

Under the documented contract serializes id and active; property order is not guaranteed. No database access occurs. Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.

BEST PRACTICES
  • Test invalid JSON, absent fields, null and wrong types. Do not concatenate unescaped input into JSON or expose secrets in errors.
  • Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.

Related content

REFERENCES
  1. TOTVS. JsonObject:FromJSON. Nil no sucesso. Accessed on 2026-10-28.
  2. TOTVS. JsonObject:ToJSON. Accessed on 2026-10-28.
  3. Atilio. dan-atilio/AdvPL. Maratona de Exemplos/Exemplo_316_Classe_JSONObject.prw. Arquivo completo consultado; adaptação editorial. Accessed on 2026-10-28.
Status
Under review
Page created on
Last reviewed on
Original language
Portuguese
Reviewed by
Usina.BR
0 approved comment(s)

Comments

There are no approved comments yet.

Sign in with Google or Microsoft to comment.

Powered by Usina Docs · Alpha