Class MongoKeyEscaper

java.lang.Object
io.agentscope.extensions.mongodb.MongoKeyEscaper

public final class MongoKeyEscaper extends Object
Recursively escapes and restores MongoDB field names inside arbitrary Map values.

MongoDB rejects '.' and a leading '$' in field names. Since state/BaseStore values are caller-supplied Map<String, Object>, their keys can contain such characters and would otherwise fail the write at encode time. This utility uses a backslash-prefix encoding on write and its inverse on read so callers are not restricted by MongoDB naming rules.

The encoding is collision-free: . → \E, $ → \$, \0 → \N, and a literal backslash → \\. Escaping the backslash itself is what guarantees a bijective round-trip — a key that literally contains \E is stored as \\E and restored to \E, never confused with an escaped dot.

  • Method Details

    • escape

      public static Map<String,Object> escape(Map<String,Object> map)
      Escapes every map key recursively (nested maps and list elements included).
      Parameters:
      map - the map to escape; must not be null
      Returns:
      a new map with escaped keys, sharing non-map leaf values with the input
    • unescape

      public static Map<String,Object> unescape(Map<String,Object> map)
      Restores every map key recursively (nested maps and list elements included).
      Parameters:
      map - the map to unescape; must not be null
      Returns:
      a new map with unescaped keys, sharing non-map leaf values with the input