/** Rigourous Test :-) */ public void test() { // モデルビーンのパッケージ名を指定してmapperをnewする Map nsmap = new HashMap(); nsmap.put("model", ""); // ""はデフォルトの名前空間 IResourceMapper mapper = new ResourceMapper(nsmap); // 配列 List elementList = new ArrayList(); Element element1 = new Element(); element1._$$text = "XYZ\\<>'\""; elementList.add(element1); Element element2 = new Element(); element2._$$text = "102"; elementList.add(element2); Element element3 = new Element(); element3._$$text = "103"; elementList.add(element3); Element element4 = new Element(); element4._$$text = "104"; elementList.add(element4); Element element5 = new Element(); element5._$$text = "105"; elementList.add(element5); Element element6 = new Element(); element6._$$text = "ABC"; elementList.add(element6); Array array = new Array(); array._element = elementList; // XMLにシリアライズ String toXML = mapper.toXML(array); System.out.println("\n【XML(array) シリアライズテスト】:"); System.out.println(toXML); // JSONにシリアライズ String toJSON = mapper.toJSON(array); System.out.println("\n【JSON(array) シリアライズテスト】:"); System.out.println(toJSON); try { // 試しにデシリアライズしてみる Array arrayFromXML = (Array) mapper.fromXML(toXML); toXML = mapper.toXML(arrayFromXML); System.out.println("\n上をデシリアライズ(XML):"); System.out.println(toXML); } catch (Exception e) { e.printStackTrace(); } try { // 試しにデシリアライズしてみる(エラーが出る) Array arrayFromJSON = (Array) mapper.fromJSON(toJSON); toJSON = mapper.toJSON(arrayFromJSON); System.out.println("\n上をデシリアライズ(JSON):"); System.out.println(toJSON); /* * // 順序を逆にする ReflexJSONTest me = new ReflexJSONTest(); List * oppsiteElement = me.opposite(arrayFromJSON.element); * arrayFromJSON.element = oppsiteElement; toJSON = * mapper.toJSON(arrayFromJSON); * System.out.println("\n上の順序を逆にしてデシリアライズ:"); * System.out.println(toJSON); */ } catch (JSONException e) { e.printStackTrace(); } }
public void jsonParser() { int iterator = 0; Manager manager = Manager.getInstance(); // Create the object of singleton Element jacksonElement = new Element(); JsonFactory jsonFactory = new JsonFactory(); try { File f = new File(FileDownload.getFileName()); JsonParser parser = jsonFactory.createParser(f); // Parse the file by Jackson parser while (!parser.isClosed()) { JsonToken jsonToken = parser.nextToken(); if (JsonToken.FIELD_NAME.equals(jsonToken)) { String fieldName = parser.getCurrentName(); jsonToken = parser.nextToken(); // The JSON file has the next fields: // location,date,name and object stock (this object // includes:name,bid,id,visible,minPrice,maxPrice) if ("location".equals(fieldName)) { manager.setLocation(parser.getValueAsString()); } if ("date".equals(fieldName)) { manager.setDate(parser.getValueAsString()); } if ("name".equals(fieldName) && (parser.getValueAsString().equals("Moscow Stock"))) manager.setName(parser.getValueAsString()); if ("bid".equals(fieldName)) { jacksonElement.setBid(parser.getValueAsDouble()); iterator++; } else if ("id".equals(fieldName)) { jacksonElement.setId(parser.getValueAsInt()); iterator++; } else if ("name".equals(fieldName) && (!parser.getValueAsString().equals("Moscow Stock"))) { jacksonElement.setName( parser.getValueAsString().substring(0, parser.getValueAsString().length() - 4)); iterator++; } else if ("minPrice".equals(fieldName)) { jacksonElement.setMinPrice(parser.getValueAsDouble()); iterator++; } else if ("maxPrice".equals(fieldName)) { jacksonElement.setMaxPrice(parser.getValueAsDouble()); iterator++; } else if ("visible".equals(fieldName)) { jacksonElement.setVisible(parser.getValueAsBoolean()); iterator++; } if (iterator == 6) { manager.setJsonList(jacksonElement); // add the data to the list jacksonElement = new Element(); iterator = 0; } } } } catch (JsonParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }