Logo

Programming-Idioms

  • Java
  • Pascal

Idiom #92 Save object into JSON file

Write the contents of the object x into the file data.json.

uses fpjsonrtti, classes;
var
  str: TMemoryStream;
  jss: string;
begin
  with TJSONStreamer.Create(nil) do try
    str := TMemoryStream.Create;
    try
      jss := ObjectToJSONString(x);
      str.Write(jss[1], length(jss));
      str.SaveToFile('data.json');
    finally
      str.Free;
    end;
  finally
    Free;
  end;
end.

TJSONStreamer uses the RTTI so only published x properties are stored.
(require '[clojure.java.io :refer [file]])
(require '[jsonista.core :refer [write-value]])
(write-value (file "data.json") x)

New implementation...