Skip to content

nginformatica/json-advpl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AdvPL JSON Parser

Copyright (C) 2016 NG Informática - TOTVS Software Partner

Instalação

Compile o arquivo src/JSON.prw no repositório e adicione o arquivo includes/json.ch à pasta de includes.

Inclusão de arquivos

#include 'json.ch'

Interfaces

Class JSON
   Method New( xData ) Constructor
   Method Parse()
   Method Stringify()
   Method Minify()
   Method File()
EndClass

Casos de uso

A maneira mais simples de utilizar é usando a função U_ParseJSON. Ela recebe o JSON atual como string e uma referência para o objeto que será a saída. Retorna .T. quando o JSON é analisado com sucesso e .F. quando há um erro sintático, também atribuindo o erro à referência à variável passada.

Parsear JSON simples

Local cJSON := '{"n": 1}'
Local oJSON

If U_ParseJSON( cJSON, @oJSON )
  ConOut( oJSON[#'n'] ) // 1
Else
  ConOut( oJSON ) // Erro como string, se houver
EndIf

Minificar um JSON existente

Local cJSON     := '{  "some":   true, [ "big", 1 ] }'
Local cMinified := JSON():New( cJSON ):Minify()
// '{"some":true,["big",1]}'

Parsear uma string JSON

Local oParser := JSON():New( '{ "data": [ { "name": "John", "age": 19 } ] }' )
oParser := oParser:Parse()

If oParser:IsJSON()
   // "John"
   oParser:Object()[#'data'][ 1 ][#'name']
   // 19
   oParser:Object()[#'data'][ 1 ][#'age']
Else
   // Em caso de erro
   ConOut( oParser:Error() )
EndIf

Você também pode acessar objetos via :Get('name') ao invés de [#'name'] e definir com :Set('name', 'Marcelo') ao invés de [#'name'] := 'Marcelo'.

Analisar arquivo JSON

{
  "key":"all",
  "description":"Todas as permissões",
  "children":[
    {
      "key":"create_order",
      "description":"Incluir O.S.",
      "children":[
        {
          "key":"create_order_corr",
          "description":"Corretiva"
        },
        {
          "key":"create_order_prev",
          "description":"Preventiva"
        }
      ]
    }
  ]
}
Local oParser := JSON():New( './main.json' )
oParser := oParser:File():Parse()
// "Corretiva"
oParser:Object()[#'children'][ 1 ][#'children'][ 1 ][#'description']

Transformar um objeto em uma string

A biblioteca provê um objeto para conversão. Use a class JSON para isso.

Local oJSON := JSONObject():New()
Local oResult

oJSON[#'data'] := { }
oJSON[#'sub' ] := 12.4

aAdd( oJSON[#'data'], JSONObject():New() )

oJSON[#'data'][ 1 ][#'name'] := 'Marcelo'
oJSON[#'data'][ 1 ][#'age']  := 19
// {"data":[{"name":"Marcelo","age":19}],"sub":12.4}

oResult := JSON():New( oJSON )
Return oResult:Stringify()

Ler e escrever dados por JSON

Function JSONFromST1
  Local aResults := { }
  Local oObj

  dbSelectArea( 'ST1' )
  dbGoTop()

  While !Eof()
    oObj := JSONObject():New()
    oObj[#'codigo'] := ST1->T1_CODFUNC
    oObj[#'nome']   := ST1->T1_NOME
    aAdd( aResults, oObj )
    dbSkip()
  End

  dbCloseArea()

  Return JSON():New( aResults ):Stringify()

Function JSONToST1( cJSON )
  Local oParser := JSON():New( cJSON )
  Local oJSON

  oParser := oParser:Parse()

  If oParser:IsJSON()
    aJSON := oParser:Object()

    dbSelectArea( 'ST1' )
    For nI := 1 To Len( aJSON )
      RecLock( 'ST1', .T. )
      ST1->T1_CODIGO := aJSON[ nI ][#'codigo']
      ST1->T1_NOME   := aJSON[ nI ][#'nome']
      MsUnlock()
    Next nI
    dbCloseArea()

  Else
    Return .F.
  EndIf

  Return .T.

Function WriteMetaData
  Return JSONToST1( '[{"nome":"Richard", "codigo": "01"},{"nome":"John","codigo":"02"}]' )

Mantenedores

Esse projeto é mantido e desenvolvido pela NG Informática ─ TOTVS Software Partner

Log de alterações

Elaborado por Marcelo Camargo em 09/06/2016

About

JSON parser written in and for TOTVS AdvPL

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages