Search in sources :

Example 1 with JSONProducer

use of beast.util.JSONProducer in project beast2 by CompEvol.

the class BeautiDoc method toJSON.

// save
private String toJSON() {
    Set<BEASTInterface> beastObjects = new HashSet<>();
    String json = new JSONProducer().toJSON(mcmc.get(), beastObjects);
    json = json.replaceFirst("\\{", "{ beautitemplate:\"" + templateName + "\", beautistatus:\"" + getBeautiStatus() + "\", ");
    return json + "\n";
}
Also used : JSONProducer(beast.util.JSONProducer) BEASTInterface(beast.core.BEASTInterface) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 2 with JSONProducer

use of beast.util.JSONProducer in project beast2 by CompEvol.

the class JSONTest method testJSONtoXMLtoJSON.

@Test
public void testJSONtoXMLtoJSON() throws Exception {
    JSONParser parser = new JSONParser();
    BEASTObject beastObject = parser.parseFile(new File(JSON_FILE));
    JSONProducer producer = new JSONProducer();
    // .replaceAll("\\s+", " ");
    String actual = producer.toJSON(beastObject).trim();
    // .replaceAll("\\s+", " ");
    String expected = BeautiDoc.load(JSON_FILE).trim();
    assertEquals("Produced JSON differs from original", expected, actual);
}
Also used : JSONProducer(beast.util.JSONProducer) JSONParser(beast.util.JSONParser) BEASTObject(beast.core.BEASTObject) File(java.io.File) Test(org.junit.Test)

Example 3 with JSONProducer

use of beast.util.JSONProducer in project beast2 by CompEvol.

the class JSONTest method testAnnotatedConstructor.

@Test
public void testAnnotatedConstructor() throws Exception {
    List<Taxon> taxa = new ArrayList<>();
    taxa.add(new Taxon("first one"));
    taxa.add(new Taxon("second one"));
    AnnotatedRunnableTestClass t = new AnnotatedRunnableTestClass(3, taxa);
    JSONProducer producer = new JSONProducer();
    String json = producer.toJSON(t);
    assertEquals(3, (int) t.getParam1());
    FileWriter outfile = new FileWriter(new File("/tmp/JSONTest.json"));
    outfile.write(json);
    outfile.close();
    JSONParser parser = new JSONParser();
    BEASTInterface b = parser.parseFile(new File("/tmp/JSONTest.json"));
    assertEquals(3, (int) ((AnnotatedRunnableTestClass) b).getParam1());
    assertEquals(2, ((AnnotatedRunnableTestClass) b).getTaxon().size());
    // test that default value for param1 comes through
    String json2 = "{version: \"2.5\",\n" + "namespace: \"beast.core:beast.evolution.alignment:beast.evolution.tree.coalescent:beast.core.util:beast.evolution.nuc:beast.evolution.operators:beast.evolution.sitemodel:beast.evolution.substitutionmodel:beast.evolution.likelihood\",\n" + "\n" + "beast: [\n" + "\n" + "\n" + "        {id: \"JSONTest\",\n" + "         spec: \"test.beast.util.AnnotatedRunnableTestClass\",\n" + "         taxon: [\n" + "                 {id: \"first one\" },\n" + "                 {id: \"second one\" }\n" + "          ]\n" + "        }\n" + "]\n" + "}";
    outfile = new FileWriter(new File("/tmp/JSONTest2.json"));
    outfile.write(json2);
    outfile.close();
    parser = new JSONParser();
    b = parser.parseFile(new File("/tmp/JSONTest2.json"));
    assertEquals(10, (int) ((AnnotatedRunnableTestClass) b).getParam1());
    assertEquals(2, ((AnnotatedRunnableTestClass) b).getTaxon().size());
    // test that array of doubles comes through in second constructor
    String json3 = "{version: \"2.5\",\n" + "namespace: \"beast.core:beast.evolution.alignment:beast.evolution.tree.coalescent:beast.core.util:beast.evolution.nuc:beast.evolution.operators:beast.evolution.sitemodel:beast.evolution.substitutionmodel:beast.evolution.likelihood\",\n" + "\n" + "beast: [\n" + "\n" + "\n" + "        {id: \"JSONTest\",\n" + "         spec: \"test.beast.util.AnnotatedRunnableTestClass\",\n" + "         array: [1.0, 2.0, 3.0]\n" + "        }\n" + "]\n" + "}";
    outfile = new FileWriter(new File("/tmp/JSONTest3.json"));
    outfile.write(json3);
    outfile.close();
    parser = new JSONParser();
    b = parser.parseFile(new File("/tmp/JSONTest3.json"));
    assertEquals(3, ((AnnotatedRunnableTestClass) b).getArray().size());
}
Also used : JSONProducer(beast.util.JSONProducer) Taxon(beast.evolution.alignment.Taxon) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) JSONParser(beast.util.JSONParser) BEASTInterface(beast.core.BEASTInterface) File(java.io.File) Test(org.junit.Test)

Aggregations

JSONProducer (beast.util.JSONProducer)3 BEASTInterface (beast.core.BEASTInterface)2 JSONParser (beast.util.JSONParser)2 File (java.io.File)2 Test (org.junit.Test)2 BEASTObject (beast.core.BEASTObject)1 Taxon (beast.evolution.alignment.Taxon)1 FileWriter (java.io.FileWriter)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1