Giter Club home page Giter Club logo

Comments (6)

michel-kraemer avatar michel-kraemer commented on June 1, 2024

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.

tmfset avatar tmfset commented on June 1, 2024

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.

michel-kraemer avatar michel-kraemer commented on June 1, 2024

I've just implemented the feature. I will release a new version of the library later today.

from bson4jackson.

tmfset avatar tmfset commented on June 1, 2024

Awesome! Thanks a lot, looking forward to using this :)

from bson4jackson.

rchom avatar rchom commented on June 1, 2024

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.

tmfset avatar tmfset commented on June 1, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.