Package io.agentscope.extensions.mongodb
Class MongoKeyEscaper
java.lang.Object
io.agentscope.extensions.mongodb.MongoKeyEscaper
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 Summary
Modifier and TypeMethodDescriptionEscapes every map key recursively (nested maps and list elements included).Restores every map key recursively (nested maps and list elements included).
-
Method Details
-
escape
Escapes every map key recursively (nested maps and list elements included).- Parameters:
map- the map to escape; must not benull- Returns:
- a new map with escaped keys, sharing non-map leaf values with the input
-
unescape
Restores every map key recursively (nested maps and list elements included).- Parameters:
map- the map to unescape; must not benull- Returns:
- a new map with unescaped keys, sharing non-map leaf values with the input
-