How to serialize a Json object? Delphi XE7

1

I need to generate an Object that represents a JSON file and Delphi is generating me the following

ERROR: 'Internal: Invalid pair name {"directory": "d: \ Folder \ Doc"}: expected type or ref'

In the line of code:

  objetoJson := UnMar.UnMarshal(oJson) as TJson;

Could anyone tell me what the problem is and how to solve it, please?

program Project2;

{$APPTYPE CONSOLE}

{$R *.res}

uses   System.SysUtils, Data.DBXJSONReflect, JSON;

type
  TJson = CLass
    diretorio: string;
  end;

var
  oJson: TJSONObject;
  objetoJson: TJson;
  UnMar: TJSONUnMarshal;
begin
  try
// Crio o Json
oJson := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes('{"diretorio": "d:\Pasta\Doc"}'),0) as TJSONObject;
// Mostra o conteudo no Json no console
Writeln('Conteudo do objeto Json: '+ojson.ToString);
UnMar := TJSONUnMarshal.Create;
try
  // Tento repassar o Objeto de Json para a Classe
  objetoJson := UnMar.UnMarshal(oJson) as TJson;
  try
    writeln(  objetoJson.diretorio);

  finally
    objetoJson.Free;
  end;
finally
  UnMar.Free;
end;
  except
on E: Exception do
begin
  Writeln('Erro ao tentar usar UmMarshal: ');
  Writeln( E.Message);
end;
  end;
  readln;
end.
    
asked by anonymous 25.09.2015 / 15:56

1 answer

1

Objects in object pascal are typed, so you need to include some references within JSON so that the UnMarshal process can identify ID and REF .

Here's the JSON example that should be passed:

'{"type":"Project2.TJsonOBJ","id":1,"fields":{"diretorio":""}}'
    
13.10.2015 / 15:24