Comments (6)
@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.
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.
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.
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.
Good idea! Allow me some time to implement this.
from cbor-java.
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)
- Tag for v0.9 HOT 2
- Published JavaDoc is outdated or incomplete
- The CBOR library is sensitive to a DOS attack HOT 2
- Fuzzing cbor-java by way of OSS-Fuzz HOT 6
- Creating a finite/fixed-size map HOT 2
- Could decoding be more ergonomic? HOT 1
- Define `Automatic-Module-Name` in manifest HOT 1
- How to performimg cbor Byte(*) ? HOT 3
- Deleted
- Release new version HOT 6
- Issue while encoding Json string HOT 1
- Difficult to decode byte array e.g. byte[] HOT 2
- Documentation for cbor-java library HOT 2
- Compatibility with peteroupc library(https://github.com/peteroupc/CBOR-Java) HOT 1
- Escaping of special characters while serialization and de-serialization HOT 3
- How to decode HOT 3
- Infinite loop during decode HOT 5
- Getting out-of-order outputs when using map builder HOT 6
- serialize and deserialize HOT 1
- Release 0.9 HOT 7
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 cbor-java.