Work with JSON

Select Text.Json and use its curated native serialization surface.

IntermediateTutorial

Select the component

set(SHARP_RUNTIME_COMPONENTS Text.Json)
target_link_libraries(app PRIVATE SharpRuntime::Text.Json)

Understand the scope

The component supplies Sharp Runtime-compatible JSON reader/writer/document and curated serialization behavior over vendored nlohmann JSON machinery. It is not a general reflection-driven clone of every System.Text.Json feature.

Round-trip an explicit native shape

#include <iostream>
#include <string>
#include <vector>

#include "System/Text/Json/JsonSerializer.hpp"

using System::Text::Json::JsonSerializer;

int main()
{
    const std::vector<int> ids{3, 7, 11};
    const std::string json = JsonSerializer::Serialize(ids);
    const auto restored =
        JsonSerializer::Deserialize<std::vector<int>>(json);

    std::cout << json << '\n';
    return restored == ids ? 0 : 1;
}
Built and run

Against the pinned source, this prints [3,7,11] and exits successfully. Conversion is compile-time nlohmann/ADL behavior—not reflection.

A safe workflow

  • Choose explicit native data shapes.
  • Validate required fields and types at the boundary.
  • Keep parse/document lifetimes clear when values reference backing storage.
  • Test comments, trailing commas, numbers, Unicode and invalid input according to the exact APIs used.

Numeric boundary

Validate aggregate member types before conversion. A current nested conversion such as a fractional JSON number inside vector<int> can truncate even though the top-level integral conversion rejects that input.

HTTP JSON

For task-shaped HTTP JSON helpers, select Net.Http.Json. It brings HTTP, Text.Json and Threading.Tasks—but it does not add HTTPS/TLS.