#1585 JsonOutStream writes invalid Json

kuipercm Sat 23 Jul 2011

Hi!

Over the last weeks I've been experimenting with Fantom, mostly very simple examples to see what it can do. So far, I like the ideas very much.

Today I tried the util::JsonOutStream functionality in util. Unfortunately, this produces invalid Json when you have (private) static or transient fields. You can also see this in the code: util::JsonOutStream.writeJsonObj (line 76) will add a comma before checking whether the field is static or transient. It should only add the comma when this check fails (not a static field and not annotated as transient).

You can also test this, using the following two classes. The test has a verifyErr around the inStream and this test is successful. When you uncomment verification part, you'll find the error.

@Serializable
class Person {
  @Transient
  private static const Str country := "United Kingdom";

  Str name;
  Str? address;
  Str? city;
  Date? birthDay := null;

  @Transient
  new make(Str name, Str address, Str city) {
    this.name = name;
    this.address = address;
    this.city = city;
  }

}

-

using util;

class JsonTest : Test {
  Void testPersonJson() {

    Person[] people := [ 
      Person("John", "Main street 55", "Silvercity"),
      Person("Sarah", "Main street 56", "Goldcity"),
      Person("Jessica", "Main street 57", "Diamondcity"),
      Person("Richard", "Main street 58", "Platinumcity"),
    ];

    File file := File(Uri.fromStr("target/persons.json"), false);
    if (!file.exists()) {
      file.create();
    }

    JsonOutStream outStream := JsonOutStream(file.out());
    outStream.writeJson(people);
    outStream.close();

    verifyErr(ParseErr#) {
      JsonInStream inStream := JsonInStream(file.in());
      Person[] parsedPeople := inStream.readJson();
      inStream.close();
    }
  }
}

The solution might simply be to swap line 76 and 77 in util::JsonOutStream. Should be checked though.

Kind regards.

brian Sat 23 Jul 2011

Hi @kuipercm

Welcome to Fantom and thanks for reporting that problem. I was able to reproduce by tweaking the existing tests. I pushed a fix to hg - changeset.

Login or Signup to reply.