Git Product home page Git Product logo

cbortree's Introduction

CborTree

CborTree is a Java library for encoding/decoding CBOR data items to/from a convenient object representation. It can also be used to convert to/from JSON and standard Java object representations.

Features

  • Full support for RFC7049
  • Partial streaming support via CoapReader and CoapWriter
  • Supports easy conversions...
    • ...to/from standard Java objects/collections/maps via toJavaObject() method
    • ...to JSON representation via toJsonString() method
    • ...to/from JSONObject/JSONArray objects (from the common org.json API)
  • Supports BigInteger
  • Supports reading and writing half (16-bit) floating-point numbers
  • RFC7049 diagnostic notation available via toString()

Current Limitations

  • Multiple tags on a single data item are not supported
  • Tags currently are limited to values no larger than Integer.MAX_VALUE (2,147,483,647).

Planned Features

  • Canonicalization as described in RFC7049 Section 3.9
  • Bytecode builder for directly generating CBOR bytecode without an intermediate object representation
  • Object builder for more easily generating CborObject trees than via manual creation.

Documentation

Example

byte[] cborBytes = new byte[] { (byte)0xd9, (byte)0xd9, (byte)0xf7,
                                (byte)0xa2, 0x61, 0x61, 0x01, 0x61,
                                0x62, (byte)0x82, 0x02, 0x03 };

CborMap cborMap = CborMap.createFromCborByteArray(cborBytes);

// Prints out the line `toString: 55799({"a":1,"b":[2,3]})`
System.out.println("toString: " + cborMap);

// Prints out the line `toJsonString: {"a":1,"b":[2,3]}`
System.out.println("toJsonString: " + cborMap.toJsonString());

CborArray cborArray = (CborArray)cborMap.get("b");

float sum = 0;

// Prints out `b: 2` and `b: 3`
for (CborObject obj : cborArray) {
    System.out.println("b: " + obj);

    if (obj instanceof CborNumber) {
        sum += ((CborNumber)obj).floatValue();
    }
}

// Prints out `Sum: 5.0`
System.out.println("Sum: " + sum);

Building and Installing

This project uses Maven for building. Once Maven is installed, you should be able to build and install the project by doing the following:

mvn verify
mvn install

Adding to Projects

Gradle:

dependencies {
  compile 'com.google.iot.cbor:cbor:0.01.02'
}

Maven:

<dependency>
  <groupId>com.google.iot.cbor</groupId>
  <artifactId>cbor</artifactId>
  <version>0.01.02</version>
</dependency>

License

CborTree is released under the Apache 2.0 license.

Copyright 2018 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Disclaimer

This is not an officially supported Google product.

cbortree's People

Contributors

darconeous avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

cbortree's Issues

Values False/True wrong!

Hello,
I just recognized, that the cbor values for True and False are wrong.
In the file CborSimple.java:
/**
* Additional data value that represents "true" when used with {@link CborMajorType#OTHER}.
*
* @see #getValue()
* @see #TRUE
*/
private static final int TYPE_TRUE = 20;

/**
 * Additional data value that represents "false" when used with {@link CborMajorType#OTHER}.
 *
 * @see #getValue()
 * @see #FALSE
 */
private static final int TYPE_FALSE = 21;

According to the Cbor Spec it should be exactly the opposite:
TYPE FALSE = 20;
TYPE TRUE = 21;
see: https://en.wikipedia.org/wiki/CBOR

Simple value Semantic
20 Boolean false
21 Boolean true
22 Null
23 Undefined

Android runtime issue

I am trying to use CborTree on android, and I run into this error when trying to read from json array:

java.lang.NoClassDefFoundError: com.google.iot.cbor.CborSimple$$ExternalSyntheticLambda0
at com.google.iot.cbor.CborSimple.create(CborSimple.java:112)
at com.google.iot.cbor.CborSimple.(CborSimple.java:70)
at com.google.iot.cbor.CborArray.createFromJSONArray(CborArray.java:216)

running from unit tests on Android Studio 2020.3.1 and CborTree dependency is
implementation 'com.google.iot.cbor:cbor:0.01.02'

Can't parse CBOR that contains negative doubles

Attempting to parse CBOR bytecode that contains negative double-precision floating point numbers will result in a parse error similar to the following:

com.google.iot.cbor.CborParseException: Additional data value was too large: 0xBFB99999A0000000

This is due to an overzealous long overflow check. Single-precision floating point numbers and half-precision floating point numbers are unaffected, as are positive double-precision floating point numbers.

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.