Comments (6)
Sounds great. We can definitely implement this in bson4jackson. Can you provide a short code snippet demonstrating what you want to do and how bson4jackson fails?
Thanks,
Michel
from bson4jackson.
Certainly! Here's an example CharacterEscapes implementation that replaces '.'s with a unicode equivalent:
class MongoCharacterEscapes extends CharacterEscapes {
public static MongoCharacterEscapes inst = new MongoCharacterEscapes();
private MongoCharacterEscapes() {}
@Override
public int[] getEscapeCodesForAscii() {
int[] escapes = CharacterEscapes.standardAsciiEscapesForJSON();
escapes['.'] = CharacterEscapes.ESCAPE_CUSTOM;
return escapes;
}
@Override
public SerializableString getEscapeSequence(int ch) {
switch(ch) {
case '.': return new SerializedString("\uff0e");
}
return null;
}
}
Then to test it:
public static void main(String[] args) throws IOException {
// Without escapes
testCharacterEscapes(false, false);
testCharacterEscapes(true, false);
// With escapes
testCharacterEscapes(false, true);
testCharacterEscapes(true, true);
}
public static void testCharacterEscapes(boolean useBson, boolean useEscapes) throws IOException {
// Our test node to be serialized.
JsonNode node = new ObjectMapper().readTree("{ \"some.field\": \"some.val\" }");
ObjectMapper mapper = useBson ? new ObjectMapper(new BsonFactory()) :
new ObjectMapper();
if (useEscapes)
mapper.getFactory().setCharacterEscapes(MongoCharacterEscapes.inst);
for (byte b : mapper.writeValueAsBytes(node))
System.out.printf("%02x ", b);
System.out.println();
}
This gives the following output (formatted so we can see the content better):
s o m e . f i e l d s o m e . v a l
// Without escapes
default 7b 22 73 6f 6d 65 2e 66 69 65 6c 64 22 3a 22 73 6f 6d 65 2e 76 61 6c 22 7d
bson 1e 00 00 00 02 73 6f 6d 65 2e 66 69 65 6c 64 00 09 00 00 00 73 6f 6d 65 2e 76 61 6c 00 00
// With escapes
default 7b 22 73 6f 6d 65 (ef bc 8e) 66 69 65 6c 64 22 3a 22 73 6f 6d 65 (ef bc 8e) 76 61 6c 22 7d
bson 1e 00 00 00 02 73 6f 6d 65 2e 66 69 65 6c 64 00 09 00 00 00 73 6f 6d 65 2e 76 61 6c 00 00
So here we can see that the default generator uses the character escapes but the bson generator produces the same output either way.
Now to be honest, I'm not really sure supporting this will solve my actual problem of not being able to store field names with '.'s in mongo. I can't remember if the java driver does the check before or after serializing the data, but I figure this might be a step along the way towards a good solution.
Thanks!
Tom
from bson4jackson.
I've just implemented the feature. I will release a new version of the library later today.
from bson4jackson.
Awesome! Thanks a lot, looking forward to using this :)
from bson4jackson.
The use case for this was to escape a '.' to a Unicode '.' and back, but I see that there is no way to do that because BsonGenerator.escapeCharacters() only supports ASCII characters.
from bson4jackson.
I don't think jackson supports unescaping characters, you could try your code with the default object mapper though and see what happens.
from bson4jackson.
Related Issues (20)
- Version 2.13 does not contain OSGi metadata anymore HOT 2
- UTF-8 conversion is significantly slower with BSON than regular JSON HOT 4
- Create 2.8 version for Jackson 2.8 HOT 9
- Reopen issue #35 HOT 1
- Leading 4 bytes are all zeroes HOT 16
- How to enable a feature in BsonGenerator.java ? HOT 8
- unexpected block in ObjectMapper.readValue() in BSON streaming situation HOT 1
- BigDecimal precision HOT 1
- BsonGenerator does not handle null values correctly HOT 3
- BsonFactory doesn't override 'createParser(DataInput in)' method HOT 2
- UnsafeByteArrayInputStream.read for bytes >= 0x80 returns negative integer, caller thinks "Oh, EOF" HOT 1
- Buffer caching for heavy multithreaded load HOT 6
- Release 2.10.x version (+ test and document compatibility with Jackson 2.10.x)
- Failed to deserialize object HOT 2
- com.fasterxml.jackson.databind.exc.MismatchedInputException: No content to map due to end-of-input HOT 2
- Support for Jackson 2.12 HOT 2
- ObjectId no longer compatible with mongodb ObjectId
- Decimal128 should extend Number & implement Comparable
- Serialization performance with large objects HOT 1
- IllegalStateException found when fuzzing bson4jackson HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bson4jackson.