Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jul 29, 2025

This PR adds support for exporting CBOR data to Extended Diagnostic Notation (EDN) format, a human-readable text representation of CBOR data as specified in the CBOR RFC.

What's Added

The implementation introduces a new CborEdn class in src/cbor/CborEdn.ts that provides methods to convert JavaScript values and CBOR-encoded data to EDN string format.

Core API

import { CborEdn, CborEncoder, CborDecoder } from '@jsonjoy.com/json-pack/lib/cbor';

const edn = new CborEdn();

// Format JavaScript values as EDN
console.log(edn.encode(42)); // "42"
console.log(edn.encode("hello")); // '"hello"'
console.log(edn.encode([1, 2, 3])); // "[1, 2, 3]"
console.log(edn.encode({a: 1, b: 2})); // '{"a": 1, "b": 2}'

// Format byte strings
console.log(edn.encode(new Uint8Array([0x01, 0x02, 0x03]))); // "h'010203'"

// Format CBOR-encoded data directly
const encoder = new CborEncoder();
const decoder = new CborDecoder();
const cborBytes = encoder.encode({name: "Alice", age: 30});
console.log(edn.formatCbor(cborBytes, decoder)); // '{"name": "Alice", "age": 30}'

Supported Data Types

The implementation handles all CBOR data types with proper EDN representation:

  • Numbers: integers, floats, BigInt, special values (NaN, ±Infinity, -0.0)
  • Strings: JSON-style escaping, Unicode support, carriage return removal per spec
  • Byte strings: hex notation (h'...') representation
  • Arrays: standard JSON-like syntax with proper nesting
  • Objects: standard JSON object notation
  • Maps: supports any key type as per CBOR specification
  • Tags: CBOR tag notation (tag(content))
  • Simple values: true/false, null/undefined, custom simple values

Examples

// Complex nested structures
const complex = {
  metadata: {
    timestamp: new JsonPackExtension(1, 1609459200), // CBOR timestamp tag
    flags: new Uint8Array([0x01, 0x02, 0x03])
  },
  users: [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}],
  settings: new Map([['debug', true], [42, 'answer']])
};

console.log(edn.encode(complex));
// Output: {"metadata": {"timestamp": 1(1609459200), "flags": h'010203'}, "users": [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}], "settings": {"debug": true, 42: "answer"}}

Testing

The implementation includes comprehensive test coverage:

  • 37 test cases covering all EDN features and edge cases
  • Integration tests with CBOR encoder/decoder roundtrip verification
  • Manual verification with complex nested data structures
  • All existing CBOR tests (767) continue to pass

Documentation

Updated the CBOR module README with EDN usage examples and API documentation.

This is an additive feature that doesn't modify any existing CBOR encoding/decoding logic, ensuring full backward compatibility.

Fixes #30.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 3 commits July 29, 2025 15:21
… export

Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
Copilot AI changed the title [WIP] Implement CBOR export to diagnostic notation Implement CBOR Extended Diagnostic Notation (EDN) export functionality Jul 29, 2025
Copilot AI requested a review from streamich July 29, 2025 15:35
Copilot finished work on behalf of streamich July 29, 2025 15:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement CBOR export to diagnostic notation

2 participants