StringMapSegmentSerializer implements SegmentSerializer<Map<String,String>> { @Override public void serialize(SegmentGenerator generator, Map<String, String> data) throws IOException, SegmentFormatException { if(generator.nextToken() == null){ generator.initToken(); } writeMap(generator,data); }  @Override void serialize(SegmentGenerator generator, Map<String, String> data);  }
@Test public void testT212(){ String data = "ST=32;CN=2011;PW=123456;MN=LD130133000015;CP=&&DataTime=20160824003817000;B01-Rtd=36.91;011-Rtd=231.0,011-Flag=N;060-Rtd=1.803,060-Flag=N&&"; Map<String,String> map = new LinkedHashMap<>(); map.put("ST","32"); map.put("CN","2011"); map.put("PW","123456"); map.put("MN","LD130133000015"); map.put("CP","&&DataTime=20160824003817000;B01-Rtd=36.91;011-Rtd=231.0,011-Flag=N;060-Rtd=1.803,060-Flag=N&&"); StringWriter writer = new StringWriter(); SegmentGenerator generator = new SegmentGenerator(writer); StringMapSegmentSerializer serializer = new StringMapSegmentSerializer(); try(SegmentGenerator g = generator) { serializer.serialize(g,map); assertEquals(writer.toString(),data); } catch (IOException e) { e.printStackTrace(); } catch (SegmentFormatException e) { e.printStackTrace(); } }