Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

51
LINES

< > BotCompany Repo | #1032561 // JSONKeyTree

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (5666L/32K).

// data = output of jsonDecode()
sclass JSONKeyTree {
  new TreeSet<S> out;
  new MultiMap<S, O> examples;
  new Map<S, IntRange> listSizes;
  
  S render(O data) {
    process(data);
    ret lines(out, key -> {
      var xmpls = examples.get(key);
      var ls = listSizes.get(key);
      if (ls != null)
        if (l(ls) == 0)
          ret key + " with " + nElements(ls.start);
        else
          ret key + " with between " + ls.start + " and " + ls.end + " elements";
      else if (nempty(xmpls))
        ret key + " = e.g. " + jsonEncode(first(xmpls));
      else
        ret key;
    });
  }
  
  Set<S> get(O data) {
    process(data);
    ret out;
  }
  
  void process(S prefix default "", O data) {
    if (data cast L) {
      S l = prefix + " is a list";
      out.add(l);
      listSizes.put(l, joinIntRanges(listSizes.get(l), emptyIntRange(l(data))));
      S s = prefix + "[]";
      for (O o : data)
        process(s, o);
    } else if (data cast Map) {
      out.add(prefix + " is a map");
      for (O key, O val : castMapToMapO(data)) {
        S s = addDotIfNempty(prefix) + key;
        out.add(s);
        process(s, val);
      }
    } else if (data == null)
      out.add(prefix + " is null");
    else {
      out.add(prefix + " is a " + shortClassName(data));
      examples.put(prefix, data);
    }
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1032561
Snippet name: JSONKeyTree
Eternal ID of this version: #1032561/15
Text MD5: 65d9005c136ac2defe9d298ad9958a0f
Transpilation MD5: fce5c573b3a934174a7f3ff1056ac1d7
Author: stefan
Category: javax / json
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-09-23 00:20:11
Source code size: 1388 bytes / 51 lines
Pitched / IR pitched: No / No
Views / Downloads: 195 / 360
Version history: 14 change(s)
Referenced in: #1032563 - jsonKeyTree
#1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)