Advertisement
Guest User

KOATUU remap

a guest
Jun 19th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 1.50 KB | None | 0 0
  1. groovy.grape.Grape.grab([group: 'com.fasterxml.jackson.core', artifact: 'jackson-databind', version: '2.9.9'])
  2. om = new com.fasterxml.jackson.databind.ObjectMapper();om.enable(com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT);
  3.  
  4. remap = { map-> levels = map.keySet().grep{ it.startsWith("level") }; if(levels.size()>1) {throw new RuntimeException(levels) }; levels.each{ map.put("children", map[it]); map.remove(it); }; map["children"].each{ remap(it) } };
  5.  
  6. koatuu = om.readValue(new File(System.getProperty("user.home")+"/Downloads/koatuu.json").newInputStream(), TreeMap.class); 0
  7. remap(koatuu); 0
  8.  
  9. restruct = { map ->
  10.  if(map["children"] !=null && map["children"].size()>0) {
  11.   codes = map["children"].collect{ it["code"].replaceAll("0+\$", "") }.sort{ it.length() };
  12.   rootCode = codes.size()>0?codes.first():null;
  13.   if(rootCode!=null) {
  14.    rootNode = map["children"].grep{ it["code"].replaceAll("0+\$", "") == rootCode }.first();
  15.    map["children"].grep{ it["code"].startsWith(rootCode) }.each{
  16.     if(it != rootNode) {
  17.      println "Restructuring "+it["code"]+" under root code "+rootNode["code"];
  18.      if(rootNode["children"] == null) {
  19.        rootNode["children"] = new LinkedList();
  20.      }
  21.      rootNode["children"].add(it);
  22.      map["children"].remove(it);
  23.     }
  24.    }
  25.    map["children"].each{ restruct(it); }
  26.   }
  27.  }
  28. }
  29.  
  30. restruct(koatuu); 0
  31.  
  32. new File(System.getProperty("user.home")+"/Downloads/koatuu_fix2.json").text = om.writerWithDefaultPrettyPrinter().writeValueAsString(koatuu); 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement