Code Monkey home page Code Monkey logo

Comments (6)

c-rack avatar c-rack commented on August 24, 2024

@neothemachine Basically, streaming encoding and decoding is already implemented. Could you please provide a small pseudo-code example what you want to achieve? Maybe I can help you to find the right direction; if not, I am sure we can find and implement a solution for your problem.

from cbor-java.

letmaik avatar letmaik commented on August 24, 2024

The README only has a "Streaming Decoding Example" and I checked the code of CborEncoder to see if it has some kind of streaming interface but couldn't find anything.

new CborEncoder(outputstream).stream()
    .add("text")                // add string
    .add(1234)                  // add integer
    .add(new byte[] { 0x10 })   // add byte array
    .addArray()                 // add array
        .add(1)
        .add("text")
        .end()
    .close();

Something like that.

from cbor-java.

c-rack avatar c-rack commented on August 24, 2024

OK, it is already there but not with a nice DSL as described. You could transform the example above to (free coded, not checked with IDE):

CborEncoder encoder = new CborEncoder(outputstream);
encoder.encode(new UnicodeString("text"));
encoder.encode(new UnsignedInteger(1234));
encoder.encode(new ByteString(new byte[] { 0x10 }));
encoder.encode(new Array().setChunked()); // start array
encoder.encode(new UnsignedInteger(1));
encoder.encode(new UnicodeString("text"));
encoder.encode(Special.BREAK); // close array
outputstream.close();

A more developer-friendly DSL would be much better, of course.

from cbor-java.

letmaik avatar letmaik commented on August 24, 2024

I see. It seems natural to me to want to use the CborBuilder as such a DSL. So CborBuilder could be made into an interface where one implementation is the current builder class and the other directly writes to a CborEncoder instance (where the relations could be set up by my proposed .stream() call).

from cbor-java.

c-rack avatar c-rack commented on August 24, 2024

Good idea! Allow me some time to implement this.

from cbor-java.

letmaik avatar letmaik commented on August 24, 2024

In parallel I am implementing a generic streaming interface that I can use both with Jackson and cbor-java and which is supposed to be compatible to both, meaning which I can easily write adapters for. I think some things in CborBuilder are actually too much for a streaming interface, so maybe this has to be split in two interfaces or so.

For reference, this is what I currently got (targeted to my use case):

/**
 * A streaming encoder interface for JSON-compatible object structures
 * with additional hints for more advanced formats like CBOR encoders.
 * 
 * This interface is a mix of cbor-java's CborBuilder and Jackson's Streaming API,
 * with the goal of being compatible to both and being able to write
 * simple adapters for them.
 *  
 * @author Maik Riechert
 */
public interface StreamingEncoder {
    class ArrayHints {
        private final Long size;
        private final Class<Number> type;
        /**
         * 
         * @param size can be null
         * @param type can be null
         */
        public ArrayHints(Long size, Class<Number> type) {
            this.size = size;
            this.type = type;
        }
        boolean hasSize() {
            return size != null;
        }
        long getSize() {
            return size;
        }
        boolean hasType() {
            return type != null;
        }
        Class<Number> getType() {
            return type;
        }
    }

    interface ArrayEncoder <T> {
        ArrayEncoder<T> add(String value) throws IOException;
        ArrayEncoder<T> add(boolean value) throws IOException;
        ArrayEncoder<T> add(int value) throws IOException;
        ArrayEncoder<T> add(long value) throws IOException;
        ArrayEncoder<T> add(float value) throws IOException;
        ArrayEncoder<T> add(double value) throws IOException;
        ArrayEncoder<ArrayEncoder<T>> startArray() throws IOException;
        ArrayEncoder<ArrayEncoder<T>> startArray(ArrayHints hints) throws IOException;
        MapEncoder<ArrayEncoder<T>> startMap() throws IOException;
        T end() throws IOException;
    }

    interface MapEncoder <T> {
        MapEncoder<T> put(String key, String value) throws IOException;
        MapEncoder<T> put(String key, boolean value) throws IOException;
        MapEncoder<T> put(String key, int value) throws IOException;
        MapEncoder<T> put(String key, long value) throws IOException;
        MapEncoder<T> put(String key, float value) throws IOException;
        MapEncoder<T> put(String key, double value) throws IOException;
        ArrayEncoder<MapEncoder<T>> startArray(String key) throws IOException;
        ArrayEncoder<MapEncoder<T>> startArray(String key, ArrayHints hints) throws IOException;
        MapEncoder<MapEncoder<T>> startMap(String key) throws IOException;
        T end() throws IOException;
    }

    MapEncoder<StreamingEncoder> startMap() throws IOException;

    void end() throws IOException;

    // at the root level we only support Maps for now.  
}

from cbor-java.

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.