@Test public void testConversion() throws JSONConverterException { ModelConverter conv = new ModelConverter(); Model mo = new DefaultModel(); Mapping m = mo.getMapping(); Node n1 = mo.newNode(); VM vm1 = mo.newVM(); m.addOnlineNode(n1); m.addReadyVM(vm1); Attributes attrs = mo.getAttributes(); attrs.put(vm1, "boot", 5); attrs.put(n1, "type", "xen"); ShareableResource rc = new ShareableResource("cpu"); rc.setConsumption(vm1, 5); rc.setCapacity(n1, 10); mo.attach(rc); String jo = conv.toJSONString(mo); System.out.println(jo); Model res = conv.fromJSON(jo); Assert.assertEquals(res, mo); Assert.assertTrue(res.contains(n1)); Assert.assertTrue(res.contains(vm1)); }
@Override public Model fromJSON(JSONObject o) throws JSONConverterException { checkKeys(o, MAPPING_LABEL, ATTRS_LABEL, VIEWS_LABEL); Model i = new DefaultModel(); fillMapping(i, (JSONObject) o.get(MAPPING_LABEL)); i.setAttributes(AttributesConverter.fromJSON(i, (JSONObject) o.get(ATTRS_LABEL))); for (Object view : (JSONArray) o.get(VIEWS_LABEL)) { i.attach(viewsConverter.fromJSON(i, (JSONObject) view)); } return i; }
ModelConverter implements JSONObjectConverter<Model> { @Override public Model fromJSON(JSONObject o) throws JSONConverterException { checkKeys(o, MAPPING_LABEL, ATTRS_LABEL, VIEWS_LABEL); Model i = new DefaultModel(); fillMapping(i, (JSONObject) o.get(MAPPING_LABEL)); i.setAttributes(AttributesConverter.fromJSON(i, (JSONObject) o.get(ATTRS_LABEL))); for (Object view : (JSONArray) o.get(VIEWS_LABEL)) { i.attach(viewsConverter.fromJSON(i, (JSONObject) view)); } return i; } }
ModelConverter implements JSONObjectConverter<Model> { @Override public Model fromJSON(JSONObject o) throws JSONConverterException { checkKeys(o, MAPPING_LABEL, ATTRS_LABEL, VIEWS_LABEL); Model i = new DefaultModel(); fillMapping(i, (JSONObject) o.get(MAPPING_LABEL)); i.setAttributes(AttributesConverter.fromJSON(i, (JSONObject) o.get(ATTRS_LABEL))); for (Object view : (JSONArray) o.get(VIEWS_LABEL)) { i.attach(viewsConverter.fromJSON(i, (JSONObject) view)); } return i; } ModelConverter(); }
ModelConverter implements JSONObjectConverter<Model> { @Override public Model fromJSON(JSONObject o) throws JSONConverterException { checkKeys(o, MAPPING_LABEL, ATTRS_LABEL, VIEWS_LABEL); Model i = new DefaultModel(); fillMapping(i, (JSONObject) o.get(MAPPING_LABEL)); i.setAttributes(AttributesConverter.fromJSON(i, (JSONObject) o.get(ATTRS_LABEL))); for (Object view : (JSONArray) o.get(VIEWS_LABEL)) { i.attach(viewsConverter.fromJSON(i, (JSONObject) view)); } return i; } ModelConverter(); ModelViewsConverter getViewsConverter(); void setModelViewConverters(ModelViewsConverter c); @Override JSONObject toJSON(Model i); @Override Model fromJSON(JSONObject o); void fillMapping(Model mo, JSONObject o); }
ModelConverter implements JSONObjectConverter<Model> { @Override public Model fromJSON(JSONObject o) throws JSONConverterException { checkKeys(o, MAPPING_LABEL, ATTRS_LABEL, VIEWS_LABEL); Model i = new DefaultModel(); fillMapping(i, (JSONObject) o.get(MAPPING_LABEL)); i.setAttributes(AttributesConverter.fromJSON(i, (JSONObject) o.get(ATTRS_LABEL))); for (Object view : (JSONArray) o.get(VIEWS_LABEL)) { i.attach(viewsConverter.fromJSON(i, (JSONObject) view)); } return i; } ModelConverter(); ModelViewsConverter getViewsConverter(); void setModelViewConverters(ModelViewsConverter c); @Override JSONObject toJSON(Model i); @Override Model fromJSON(JSONObject o); void fillMapping(Model mo, JSONObject o); static final String MAPPING_LABEL; static final String VIEWS_LABEL; static final String ATTRS_LABEL; }