Encode a packet and inspect a validation finding
Native SDK. Append --roundtrip example.json to your language's run command from the SDK root. From examples/go or examples/csharp, use ../../example.json. For setup and build commands, see Decode your first native packet.
For example, from examples/go on Linux run go run . ../../lib/misb0601.so ../../example.klv --roundtrip ../../example.json. From examples/csharp on Windows run dotnet run -- ../../lib/misb0601.dll ../../example.klv --roundtrip ../../example.json.
Encode the sample fields
The bundled example.json contains the fields from example.klv, with the precision timestamp as an integer and the scaled values at full precision. The examples pass those JSON bytes to the revision 19 encoder. It sorts the tags and calculates a new checksum.
{
"2": 1795433587745520,
"65": 19,
"3": "Test mission",
"4": "24",
"13": 41.093374323609,
"14": -104.8687724978052,
"15": 2899.9328603036543,
"5": 147.99908445868618,
"10": "Test platform",
"1": 26780
}
The example driver calls the helper below when you supply the flag.
- C
- C++
- C#
- Go
- Java
- Python
#pragma once
#include "misb0601.h"
#include <stdio.h>
static int roundtrip(uint8_t *input, size_t input_size) {
misb0601_buffer packet = {0}, fields = {0}, report = {0};
// Encode the tag-keyed JSON as a revision 19 KLV packet with a new checksum.
int ok = Misb0601EncodeJSON(input, input_size, MISB0601_REVISION_19,
MISB0601_SORT_BY_TAG, &packet) == MISB0601_STATUS_OK;
// Decode the complete output buffer to inspect the encoded fields.
if (ok) ok = Misb0601DecodeJSON(packet.data, packet.len, MISB0601_SORT_BY_TAG,
MISB0601_JSON_KEYS_TAGS, MISB0601_JSON_COMPACT, MISB0601_TIMESTAMPS_RAW, &fields) == MISB0601_STATUS_OK;
// Omit the last byte to get a packet-decode finding in the validation report.
if (ok) ok = Misb0601ValidatePacket(packet.data, packet.len - 1, NULL, &report) == MISB0601_STATUS_OK;
if (ok) {
fwrite(fields.data, 1, fields.len, stdout);
putchar('\n');
fwrite(report.data, 1, report.len, stdout);
putchar('\n');
}
// Release all SDK-owned output buffers, including after a failed call.
Misb0601BufferFree(&report);
Misb0601BufferFree(&fields);
Misb0601BufferFree(&packet);
return ok;
}
#pragma once
#include "misb0601.hpp"
#include <iostream>
inline void roundtrip(misb0601::Library &lib, misb0601::DecoderOptions options, const std::string &json) {
// Encode with the default revision (19) and generate a new checksum.
auto packet = lib.EncodeJson(json);
// Decode the complete output. The wrapper frees the buffers as they go out of scope.
std::cout << lib.DecodeJson(packet, options).ReadString() << '\n';
// Pass one byte less to validation so the report contains a packet-decode finding.
std::cout << lib.ValidatePacketJson(packet.Data(), packet.Size() - 1) << '\n';
}
using System.Text.Json;
using LibMisb.Misb0601;
static class RoundTrip
{
internal static void Run(Misb0601Library library, Misb0601Decoder decoder, ReadOnlySpan<byte> json)
{
// Encode the tag-keyed JSON as revision 19 and generate a new checksum.
using var packet = library.CreateEncoder(
new Misb0601EncoderOptions(SortMode: Misb0601SortMode.SortByTag)).EncodeJson(json);
// Decode the complete output before attempting validation.
using var fields = decoder.DecodeJson(packet);
Console.WriteLine(fields.ReadUtf8());
// Pass a slice without the last byte to produce a packet-decode finding.
using var report = JsonDocument.Parse(library.ValidatePacketJson(packet.AsSpan()[..^1]));
// Read the verdict and findings from the report.
var root = report.RootElement;
Console.WriteLine(root.GetProperty("valid").GetBoolean() ? "valid" : "invalid");
if (root.TryGetProperty("findings", out var findings) && findings.ValueKind == JsonValueKind.Array)
foreach (var finding in findings.EnumerateArray())
Console.WriteLine($"{finding.GetProperty("rule_id")} {finding.GetProperty("message")}");
}
}
package main
import (
"fmt"
sdk "github.com/impleotv/libmisb-sdk-go/misb0601abi"
)
func roundtrip(lib *sdk.Library, decoder *sdk.Decoder, input []byte) {
// Encode the tag-keyed JSON with the default revision (19) and a new checksum.
encoder := lib.CreateEncoder(sdk.DefaultEncoderOptions())
packet, err := encoder.EncodeJSON(input)
must(err)
// Decode the complete output packet to inspect its fields.
fields, err := decoder.DecodeJSON(packet)
must(err)
fmt.Println(string(fields))
// Omit the final byte to produce a packet-decode finding.
report, err := lib.ValidatePacket(packet[:len(packet)-1], sdk.ValidationOptions{})
must(err)
// Read the verdict and findings from the report.
if report.Valid {
fmt.Println("valid")
} else {
fmt.Println("invalid")
}
for _, finding := range report.Findings {
fmt.Println(finding.RuleID, finding.Message)
}
}
import java.lang.foreign.*;
import java.lang.invoke.MethodHandle;
import static java.lang.foreign.ValueLayout.*;
class RoundTrip {
static void run(DecodePacket api, Arena arena, byte[] json) throws Throwable {
// Bind the encoder and validator and allocate the native buffers.
MethodHandle encode = api.bind("Misb0601EncodeJSON",
FunctionDescriptor.of(JAVA_INT, ADDRESS, JAVA_LONG, JAVA_INT, JAVA_INT, ADDRESS));
MethodHandle validate = api.bind("Misb0601ValidatePacket",
FunctionDescriptor.of(JAVA_INT, ADDRESS, JAVA_LONG, ADDRESS, ADDRESS));
MemorySegment input = arena.allocateFrom(JAVA_BYTE, json);
MemorySegment encoded = arena.allocate(DecodePacket.BUFFER);
MemorySegment report = arena.allocate(DecodePacket.BUFFER);
try {
// Encode with revision 19 and tag sorting. The encoder calculates the checksum.
api.check(arena, (int) encode.invokeExact(input, input.byteSize(), 19, 1, encoded));
// Decode the complete encoded packet to inspect the fields.
MemorySegment packet = DecodePacket.data(encoded);
api.printJSON(packet, arena);
// Omit the last byte so validation produces a packet-decode finding.
api.check(arena, (int) validate.invokeExact(packet, packet.byteSize() - 1, MemorySegment.NULL, report));
System.out.println(DecodePacket.text(report));
} finally {
// Free the SDK-owned buffers. The arena manages the buffer descriptors.
api.free.invokeExact(report);
api.free.invokeExact(encoded);
}
}
}
def roundtrip(library, decoder, json_bytes):
# Encode the tag-keyed JSON as revision 19 and generate a new checksum.
packet = library.create_encoder().encode_json(json_bytes)
# Decode the complete output packet to inspect the fields.
print(decoder.decode_json(packet).decode())
# Omit the last byte to produce a packet-decode finding.
report = library.validate_packet(packet[:-1])
# Read the verdict and findings from the report.
print("valid" if report.valid else "invalid")
for finding in report.findings:
print(finding.rule_id, finding.message)
Inspect the encoded buffer
The decoder walkthrough shows the original 87-byte packet. Its BER length is 46 in hex (70 value bytes), and its checksum ends in 68 9C.

In demo mode, the encoder omits sensor true altitude (tag 15). The resulting 83-byte buffer has BER length 42 (66 value bytes) and checksum 92 4A (37450). With a licensed encoder, the buffer includes altitude, has 87 bytes, and ends in D6 2E (54830) in sorted order.
0000 06 0E 2B 34 02 0B 01 01 0E 01 03 01 01 00 00 00
0010 42 02 08 00 06 60 EF F5 37 22 F0 03 0C 54 65 73
0020 74 20 6D 69 73 73 69 6F 6E 04 02 32 34 05 02 69
0030 3E 0A 0D 54 65 73 74 20 70 6C 61 74 66 6F 72 6D
0040 0D 04 3A 71 A4 18 0E 04 B5 6D 38 FB 41 01 13 01
0050 02 92 4A
The examples decode the complete output buffer and print its fields. Look for "3":"Test mission", timestamp 1795433587745520, and revision 19.
Inspect a validation finding
The examples pass the encoded buffer to the validator with the last byte omitted. The packet still declares its full value length, so the validator reports a truncated packet. Pass the complete buffer when validating application data.
The report has valid: false. The first finding has rule ID packet-decode, severity error, and a message identifying the packet decode failure. The C, C++, and Java examples print the JSON report. The C#, Go, and Python examples print the verdict followed by each finding. A successful validation call means a report was produced. Use its valid field to decide whether to accept the packet.