• Stars
    star
    205
  • Rank 191,264 (Top 4 %)
  • Language
    C#
  • License
    The Unlicense
  • Created about 11 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A C# implementation of Concise Binary Object Representation (RFC 8949).

CBOR

NuGet Status

Download source code: ZIP file


A C# implementation of Concise Binary Object Representation, a general-purpose binary data format defined in RFC 8949. According to that RFC, CBOR's data model "is an extended version of the JSON data model", supporting many more types of data than JSON. "CBOR was inspired by MessagePack", but "is not intended as a version of or replacement for MessagePack."

This implementation was written by Peter O. and is released to the Public Domain under the CC0 Declaration.

This implementation also doubles as a reader and writer of JSON, and can convert data from JSON to CBOR and back.

Finally, this implementation supports arbitrary-precision binary and decimal floating-point numbers and rational numbers with arbitrary-precision components.

Source code is available in the project page.

Note that after version 4.5x, the CBOR library's repository will stop including special projects for .NET 2.0 and .NET 4.0, leaving the .NET-Standard project for building the library.

How to Install

Starting with version 0.21.0, the C# implementation is available in the NuGet Package Gallery under the name PeterO.Cbor. To install this library as a NuGet package, enter Install-Package PeterO.Cbor in the NuGet Package Manager Console.

Documentation

This library defines one class, called CBORObject, that allows you to read and write CBOR objects to and from data streams and byte arrays, and to convert JSON text to CBOR objects and back.

See the C# (.NET) API documentation.

The C# implementation is designed as a Portable Class Library.

Other Sites

Examples

The following shows certain use examples of this library. Additional examples can be found in the API documentation.

Creating a map and converting that map to CBOR bytes and a JSON string:

// The following creates a CBOR map and adds
// several kinds of objects to it
var cbor = CBORObject.NewMap()
   .Add("item", "any string")
   .Add("number", 42)
   .Add("map", CBORObject.NewMap().Add("number", 42))
   .Add("array", CBORObject.NewArray().Add(999f).Add("xyz"))
   .Add("bytes", new byte[] { 0, 1, 2 });
// The following converts the map to CBOR
byte[] bytes = cbor.EncodeToBytes();
// The following converts the map to JSON
string json = cbor.ToJSONString();
Console.WriteLine(json);

Creating a map and converting that map to canonical CBOR bytes (for WebAuthn and other purposes) and a .NET dictionary:

// The following creates a CBOR map and adds
// several kinds of objects to it
var cbor = CBORObject.NewMap()
   .Add("item", "any string")
   .Add("foo", "another string")
   .Add("quux", "a third string");
// The following converts the map to canonical CBOR
byte[] bytes = cbor.EncodeToBytes(CBOREncodeOptions.DefaultCtap2Canonical);
// The following converts the map to a dictionary
var dict = cbor.ToObject<IDictionary<string,string>>();
Console.WriteLine(dict.Count);

Reading data from a file (C#). Note that all the examples for reading and writing to files assume that the platform supports file I/O; the portable class library doesn't make that assumption.

 // Read all the bytes from a file and decode the CBOR object
 // from it.  However, there are two disadvantages to this approach:
 // 1.  The byte array might be very huge, so a lot of memory to store
 // the array may be needed.
 // 2.  The decoding will succeed only if the entire array,
 // not just the start of the array, consists of a CBOR object.
 var cbor = CBORObject.DecodeFromBytes(
    File.ReadAllBytes("object.cbor"), CBOREncodeOptions.Default);

Another example of reading data from a file:

 // C#
 // Open the file stream
 using (var stream = new FileStream("object.cbor", FileMode.Open)) {
    // Read the CBOR object from the stream
    var cbor = CBORObject.Read(stream);
    // At this point, the object is read, but the file stream might
    // not have ended yet.  Here, the code may choose to read another
    // CBOR object, check for the end of the stream, or just ignore the
    // rest of the file.  The following is an example of checking for the
    // end of the stream.
    if (stream.Position != stream.Length) {
      // The end of the stream wasn't reached yet.
    } else {
      // The end of the stream was reached.
    }
 }

If a byte array contains multiple CBOR objects, the byte array should be wrapped in a MemoryStream and the stream used to read the objects, as DecodeFromBytes assumes the array contains only one CBOR object. Here is an example.

 // C#
 // Create a memory stream with a view of the byte array
 using (var stream = new MemoryStream(byteArray)) {
    // Read the CBOR object from the stream
    var cbor = CBORObject.Read(stream);
    // The rest of the example follows the one given above.
 }

Writing CBOR data to a file (C#):

// This example assumes that the variable "cbor" refers
// to a CBORObject object.
using (var stream = new FileStream("object.cbor", FileMode.Create)) {
   cbor.WriteTo(stream);
}

Writing multiple objects to a file, including arbitrary objects (the resulting file is also called a CBOR sequence):

// C#
// This example writes a sequence of objects in CBOR
// format to the same file.
using (var stream = new FileStream("object.cborseq", FileMode.Create)) {
   CBORObject.Write(true, stream);
   CBORObject.Write(422.5, stream);
   CBORObject.Write("some string", stream);
   CBORObject.Write(CBORObject.Undefined, stream);
   CBORObject.NewArray().Add(42).WriteTo(stream);
}

Reading JSON from a file:

 // Open the file stream
 using (var stream = new FileStream("object.json", FileMode.Open)) {
    // Read the JSON object from the stream
    // as a CBOR object
    var cbor = CBORObject.ReadJSON(stream);
 }

Writing a CBOR object as JSON:

// This example assumes that the variable "cbor" refers
// to a CBORObject object.
// NOTE: Specifying Encoding.UTF8 as the third parameter
// would add a byte order mark to the beginning of the text,
// but conforming JSON implementations are forbidden from
// adding it this way in JSON texts they generate.
File.WriteAllText(
  "object.json",
  cbor.ToJSONString(),
  new System.Text.Encoding.UTF8Encoding(false));

// This is an alternative way to write the CBOR object
// and is supported since version 1.2.
using (var stream = new FileStream("object2.json", FileMode.Create)) {
    // Write the CBOR object as JSON; here, a byte order
    // mark won't be added
    cbor.WriteJSONTo(stream);
}
// Version 1.2 and later support a third way to write
// objects to JSON: the CBORObject.WriteJSON method
using (var stream = new FileStream("object3.json", FileMode.Create)) {
   CBORObject.WriteJSON("some string", stream);
}
using (var stream = new FileStream("object4.json", FileMode.Create)) {
   CBORObject.WriteJSON(cbor, stream);
}
using (var stream = new FileStream("object5.json", FileMode.Create)) {
   CBORObject.WriteJSON(true, stream);
}
using (var stream = new FileStream("object6.json", FileMode.Create)) {
   CBORObject.WriteJSON(42, stream);
}

There are several ways to check whether a CBOR object is a 32-bit integer. Which one to use depends on the application's needs. Some of them follow (written in C#).

/* Accept any untagged CBOR integer
  that can fit the Int32 range */
if(!cbor.IsTagged && cbor.Type==CBORType.Integer &&
   cbor.CanValueFitInInt32()) {
  return cbor.AsInt32();
}
/* Accept any CBOR integer, tagged or not, that
 can fit the Int32 range */
if(cbor.Type==CBORType.Integer && cbor.CanValueFitInInt32()) {
  return cbor.AsInt32();
}
/* Accept any CBOR integer or floating-point number,
 tagged or not, that is an integer within the Int32 range */
if((cbor.Type==CBORType.Integer || cborType==CBORType.FloatingPoint) ||
   cbor.Untag().AsNumber().CanValueFitInInt32()) {
  return cbor.AsInt32();
}
/* Accept any CBOR object representing an integer number
   that can fit the Int32 range */
if(cbor.IsNumber && cbor.AsNumber().CanValueFitInInt32()) {
  return cbor.AsInt32();
}

The following example illustrates a custom strategy for converting objects of a given class into CBOR objects.

    public class CPOD3 {
       public string Aa { get; set; }
       public string Bb { get; set; }
       public string Cc { get; set; }
    }

    private class CPOD3Converter : ICBORToFromConverter<CPOD3> {
       public CBORObject ToCBORObject(CPOD3 cpod) {
          return CBORObject.NewMap()
             .Add(0,cpod.Aa)
             .Add(1,cpod.Bb)
             .Add(2,cpod.Cc);
       }
       public CPOD3 FromCBORObject(CBORObject obj) {
          if (obj.Type!=CBORType.Map) {
             throw new CBORException();
          }
          var ret=new CPOD3();
          ret.Aa=obj[0].AsString();
          ret.Bb=obj[1].AsString();
          ret.Cc=obj[2].AsString();
          return ret;
       }
    }

    //////////
    //  And in the code...

       var cp2=new CPOD3();
       cp2.Aa="AA";
       cp2.Bb="BB";
       cp2.Cc="CC";
       var tm=new CBORTypeMapper().AddConverter(
           typeof(CPOD3),
           new CPOD3Converter());
       // Serialize CBOR object, passing the type mapper
       var cbor=CBORObject.FromObject(cp2,tm);
       // Deserialize CBOR object, passing the type mapper
       cp2=cbor.ToObject<CPOD3>(tm);

NOTE: All code samples in this section are released to the Public Domain, as explained in https://creativecommons.org/publicdomain/zero/1.0/.

Demo

Go to https://github.com/peteroupc/Calculator for source code to a demo of the CBOR library in the form of a calculator.

About

Written in 2013-2019 by Peter O.

Any copyright to this work is released to the Public Domain. In case this is not possible, this work is also licensed under Creative Commons Zero (CC0): https://creativecommons.org/publicdomain/zero/1.0/

Release Notes

See History.md for release notes.

Specifications

Here are specifications by this implementation's author on certain CBOR tags:

Acknowledgments

  • Carsten Bormann reviewed this library and gave helpful suggestions.
  • Anders Gustafsson converted this library to a Portable Class Library.

I thank all users who sent issues to this repository.

More Repositories

1

peteroupc.github.io

Pages about my projects
HTML
52
star
2

Numbers

A C# implementation of arbitrary-precision numbers and arithmetic
C#
51
star
3

CBOR-Java

A Java implementation of Concise Binary Object Representation (RFC 8949)
Java
42
star
4

html3dutil

A public domain JavaScript library to ease developing HTML 3D apps
JavaScript
30
star
5

numbers-java

A Java implementation of arbitrary-precision arithmetic classes
Java
6
star
6

colorpicker

A color picker in JavaScript. Supports adapters for integrating other popular color pickers, and can use native color selection controls. In the public domain.
JavaScript
6
star
7

TurtleParser

A Turtle RDF parser for C# and Java.
Java
4
star
8

HtmlParser

HTML5 Parser for Java
Java
4
star
9

Calculator

A calculator for .NET, intended to demonstrate the arbitrary-precision arithmetic capabilities of the CBOR library.
C#
4
star
10

CBOR-PHP

A PHP implementation of Concise Binary Object Representation (RFC 7049)
PHP
3
star
11

URIUtility

URIUtility class for .NET and Java
C#
3
star
12

MailLib

A portable library in C# and Java for parsing and generating Internet mail messages.
Java
2
star
13

ColorWallpaper

Colored Boxes Live Wallpaper for Android
Java
2
star
14

colorvalidator

Functions for server-side validation of CSS colors, in PHP, Python, Ruby, C#, and Java. In the public domain.
C#
2
star
15

markdownsyntax

This document is intended to move Markdown towards a standard syntax.
2
star
16

BigNumber

An arbitrary-precision arithmetic library for JavaScript
JavaScript
2
star
17

TurtleParserJava

A Turtle RDF parser for Java. In the public domain under CC0.
Java
1
star
18

Fuzzer

Generates slightly altered versions of data files, for testing algorithms that parse file formats
C#
1
star
19

StructureBuilder

Generates a data structure class from C# and VB.NET comments
C#
1
star
20

DataUtilities

DataUtilities class for .NET and Java
Java
1
star
21

petero-csharp

A collection of utility classes in C#. In the public domain.
C#
1
star
22

encoding

A library in C# and Java that implements character encodings used in Web pages and email.
C#
1
star
23

classic-wallpaper

Repository on a challenge for open-source tileable wallpapers with a limited color palette and a limited pixel size.
Python
1
star