Coder Social home page Coder Social logo

json-advpl's Introduction

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

json-advpl's People

Contributors

haskellcamargo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.