@Test public void testDeserialize(){ String data = "##0136ST=32;CN=2011;PW=123456;MN=LD130133000015;CP=&&DataTime=20160824003817;B01-Rtd=36.91;011-Rtd=231.0,011-Flag=N;060-Rtd=1.803,060-Flag=N&&4980\r\n"; StringReader reader = new StringReader(data); T212Parser t212Parser = new T212Parser(reader); T212Configurator configurator = new T212Configurator(); int vFeature = Feature.collectFeatureDefaults(VerifyFeature.class); vFeature = vFeature | ALLOW_MISSING_FIELD.getMask(); vFeature = vFeature | ALLOW_VALUE_NOT_IN_RANGE.getMask(); configurator.setVerifyFeature(vFeature); configurator.setValidator(Validation.buildDefaultValidatorFactory().getValidator()); configurator.setParserFeature(Feature.collectFeatureDefaults(ParserFeature.class)); CpDataLevelMapDeserializer deserializer = new CpDataLevelMapDeserializer(); deserializer.configured(configurator); try (T212Parser parser = t212Parser) { Map<String, Object> map = deserializer.deserialize(parser); assertNotNull(map); assertEquals(map.get("ST"),"32"); assertEquals(map.get("CN"),"2011"); assertEquals(map.get("PW"),"123456"); assertEquals(map.get("MN"),"LD130133000015"); map = (Map<String, Object>) map.get("CP"); assertEquals(map.get("DataTime"),"20160824003817"); assertEquals(map.get("B01-Rtd"),"36.91"); assertEquals(map.get("011-Rtd"),"231.0"); assertEquals(map.get("011-Flag"),"N"); assertEquals(map.get("060-Rtd"),"1.803"); assertEquals(map.get("060-Flag"),"N"); } catch (T212FormatException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
@SuppressWarnings("Duplicates") @Override public Map<String, Object> deserialize(T212Parser parser) throws IOException, T212FormatException { parser.readHeader(); int len = parser.readInt32(10); if(len == -1){ T212FormatException.length_not_range(PacketElement.DATA_LEN,len,4,4); } if(DATA_LEN_RANGE.enabledIn(verifyFeature)){ VerifyUtil.verifyRange(len,0,1024, PacketElement.DATA_LEN); } char[] data = parser.readData(len); int crc = parser.readInt32(16); if(DATA_CRC.enabledIn(verifyFeature)){ if(crc == -1 || crc16Checkout(data,len) != crc){ T212FormatException.crc_verification_failed(PacketElement.DATA,data,crc); } } parser.readFooter(); return deserialize(data); }
CpDataLevelMapDeserializer implements T212Deserializer<Map<String,Object>>, Configured<CpDataLevelMapDeserializer> { @SuppressWarnings("Duplicates") @Override public Map<String, Object> deserialize(T212Parser parser) throws IOException, T212FormatException { parser.readHeader(); int len = parser.readInt32(10); if(len == -1){ T212FormatException.length_not_range(PacketElement.DATA_LEN,len,4,4); } if(DATA_LEN_RANGE.enabledIn(verifyFeature)){ VerifyUtil.verifyRange(len,0,1024, PacketElement.DATA_LEN); } char[] data = parser.readData(len); int crc = parser.readInt32(16); if(DATA_CRC.enabledIn(verifyFeature)){ if(crc == -1 || crc16Checkout(data,len) != crc){ T212FormatException.crc_verification_failed(PacketElement.DATA,data,crc); } } parser.readFooter(); return deserialize(data); } }
CpDataLevelMapDeserializer implements T212Deserializer<Map<String,Object>>, Configured<CpDataLevelMapDeserializer> { @SuppressWarnings("Duplicates") @Override public Map<String, Object> deserialize(T212Parser parser) throws IOException, T212FormatException { parser.readHeader(); int len = parser.readInt32(10); if(len == -1){ T212FormatException.length_not_range(PacketElement.DATA_LEN,len,4,4); } if(DATA_LEN_RANGE.enabledIn(verifyFeature)){ VerifyUtil.verifyRange(len,0,1024, PacketElement.DATA_LEN); } char[] data = parser.readData(len); int crc = parser.readInt32(16); if(DATA_CRC.enabledIn(verifyFeature)){ if(crc == -1 || crc16Checkout(data,len) != crc){ T212FormatException.crc_verification_failed(PacketElement.DATA,data,crc); } } parser.readFooter(); return deserialize(data); }  }
CpDataLevelMapDeserializer implements T212Deserializer<Map<String,Object>>, Configured<CpDataLevelMapDeserializer> { @SuppressWarnings("Duplicates") @Override public Map<String, Object> deserialize(T212Parser parser) throws IOException, T212FormatException { parser.readHeader(); int len = parser.readInt32(10); if(len == -1){ T212FormatException.length_not_range(PacketElement.DATA_LEN,len,4,4); } if(DATA_LEN_RANGE.enabledIn(verifyFeature)){ VerifyUtil.verifyRange(len,0,1024, PacketElement.DATA_LEN); } char[] data = parser.readData(len); int crc = parser.readInt32(16); if(DATA_CRC.enabledIn(verifyFeature)){ if(crc == -1 || crc16Checkout(data,len) != crc){ T212FormatException.crc_verification_failed(PacketElement.DATA,data,crc); } } parser.readFooter(); return deserialize(data); }  @Override void configured(Configurator<CpDataLevelMapDeserializer> configurator); @SuppressWarnings("Duplicates") @Override Map<String, Object> deserialize(T212Parser parser); Map<String, Object> deserialize(char[] data); void setVerifyFeature(int verifyFeature); void setSegmentParserConfigurator(Configurator<SegmentParser> segmentParserConfigurator); void setSegmentDeserializer(SegmentDeserializer<Map<String, Object>> segmentDeserializer); void setValidator(Validator validator); }
CpDataLevelMapDeserializer implements T212Deserializer<Map<String,Object>>, Configured<CpDataLevelMapDeserializer> { @SuppressWarnings("Duplicates") @Override public Map<String, Object> deserialize(T212Parser parser) throws IOException, T212FormatException { parser.readHeader(); int len = parser.readInt32(10); if(len == -1){ T212FormatException.length_not_range(PacketElement.DATA_LEN,len,4,4); } if(DATA_LEN_RANGE.enabledIn(verifyFeature)){ VerifyUtil.verifyRange(len,0,1024, PacketElement.DATA_LEN); } char[] data = parser.readData(len); int crc = parser.readInt32(16); if(DATA_CRC.enabledIn(verifyFeature)){ if(crc == -1 || crc16Checkout(data,len) != crc){ T212FormatException.crc_verification_failed(PacketElement.DATA,data,crc); } } parser.readFooter(); return deserialize(data); }  @Override void configured(Configurator<CpDataLevelMapDeserializer> configurator); @SuppressWarnings("Duplicates") @Override Map<String, Object> deserialize(T212Parser parser); Map<String, Object> deserialize(char[] data); void setVerifyFeature(int verifyFeature); void setSegmentParserConfigurator(Configurator<SegmentParser> segmentParserConfigurator); void setSegmentDeserializer(SegmentDeserializer<Map<String, Object>> segmentDeserializer); void setValidator(Validator validator);  }