Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ add_library(lootlocker_core STATIC
src/core/header_map.cpp
src/core/request_id.cpp
src/core/rate_limiter.cpp
src/core/json.cpp
src/core/executor.cpp
src/core/client.cpp
)
Expand Down
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,72 @@ already cached can block for the length of the lookup. The transfer itself is po
name resolution is not. If that matters, resolve once at startup or inject a transport whose
resolver is asynchronous.

## JSON

The SDK parses JSON with [nlohmann/json](https://github.com/nlohmann/json), vendored as a
submodule pinned to `v3.11.3` and included by exactly one translation unit. **You never see
it.** No public header includes it, and no public type names it.

That is deliberate: it keeps our parser choice out of the API, so changing it later is not a
breaking change for you.

### Reading a field the SDK does not model

Use `RawBody()`. Every result carries the response body exactly as the server sent it, on
success and on failure, so you can parse it with whatever you already have:

```cpp
lootlocker::broadcasts::List(ll, [](lootlocker::Result<std::vector<lootlocker::Broadcast>> r) {
if (!r.Ok()) {
// The body is here too, so you can see what the server actually said.
my_log(r.RawBody());
return;
}
my_own_parser(r.RawBody()); // your parser, your types
});
```

This is the intended answer to "the SDK is missing a field". It is not a workaround.

### Custom decoding for a type

If you need a field decoded differently from the default, write an ADL `from_json` next to
your type. It takes a `lootlocker::JsonValue`, which is a parser-independent handle — it
exposes the shape of the value and nothing about which library produced it:

```cpp
namespace my_game {

struct PlayerStats {
std::uint64_t total_score = 0;
std::string rank;
};

void from_json(const lootlocker::JsonValue& j, PlayerStats& out)
{
// `IsUnsigned()` matters: reading a uint64 through AsDouble() loses precision above
// 2^53, which is the one hazard the JSON spike found.
if (j["total_score"].IsUnsigned()) {
out.total_score = j["total_score"].AsUInt64();
}
out.rank = j["rank"].AsString();
}

} // namespace my_game
```

`JsonValue` gives you `GetKind()`, `IsInteger()`, `IsUnsigned()`, the typed `As*` reads,
`operator[]`, `HasKey()`, `Size()` and `At()`. An absent key is `Kind::Invalid`, which is
distinct from `Kind::Null` — so "the field is missing" and "the field is null" are different
answers, and you can tell them apart.

### There is no parser seam

Unlike the transport or the state writer, the JSON parser is not injectable. It is internal
machinery rather than an ecosystem-docking point: every adopter needs it, none needs a
different one, and a seam with no consumer is a seam nobody maintains. If you need a
different parser, `RawBody()` gives you the bytes.

## What is in v1

Guest, white label and Steam authentication, plus broadcasts. Presence, offline mode,
Expand Down
107 changes: 107 additions & 0 deletions include/lootlocker/json_value.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#pragma once

// A parser-independent view over a decoded JSON value (D4, D27).
//
// This exists so an adopter can write a custom serialiser without taking a compile-time
// dependency on our parser choice. D4 chose nlohmann/json *behind a pimpl* precisely so the
// library is not part of our API; handing adopters `nlohmann::json` in a `from_json`
// signature would undo that in one line, and swapping the parser (which D4 names as the
// fallback if the pimpl is ever dropped) would become a breaking change.
//
// So this is a handle, not a container: it exposes the shape (kind, members, elements) and
// nothing about which library produced it. The implementation is in `src/core/json.cpp`,
// which is the only translation unit that includes the JSON library.

#include <lootlocker/result.h>

#include <cstddef>
#include <cstdint>
#include <memory>
#include <string>

namespace lootlocker {

class JsonValue;

namespace core {
class JsonParser;

// Declared here so the friend declarations below can name them. Defined in
// `src/core/json.cpp`.
Result<JsonValue> ParseJson(const std::string& text);
std::string SerializeJson(const JsonValue& value);
} // namespace core

class JsonValue {
public:
// `Invalid` is not a JSON kind: it is what an absent key or an out-of-range index
// returns, so a caller can tell "the key is missing" from "the key is null". Those are
// different answers and conflating them is how a decoder silently defaults a field.
enum class Kind { Invalid, Null, Bool, Number, String, Array, Object };

JsonValue();
~JsonValue();

JsonValue(const JsonValue&);
JsonValue& operator=(const JsonValue&);
JsonValue(JsonValue&&) noexcept;
JsonValue& operator=(JsonValue&&) noexcept;

// False for the default-constructed value and for an absent key.
bool IsValid() const;

Kind GetKind() const;

// Typed reads. Each returns a default (false, 0, empty) when the value is not of that
// kind, so a caller that has already checked `GetKind()` pays nothing and a caller that
// has not gets a defined answer rather than undefined behaviour.
bool AsBool() const;
double AsDouble() const;
std::int64_t AsInt64() const;
std::uint64_t AsUInt64() const;
std::string AsString() const;

// Whether a `Number` is an integer rather than a float, and whether it is unsigned.
//
// Exposed because a custom serialiser needs the same distinction the SDK's own decoders
// make: a `uint64` field must not be read through `AsDouble()`, which loses precision
// above 2^53. Without these, an adopter writing a `from_json` for such a field has to
// guess from the value, and guessing is how the 2^53 hazard comes back.
//
// Both are false for anything that is not a `Number`.
bool IsInteger() const;
bool IsUnsigned() const;

// Object access. Returns an invalid value when the key is absent or this is not an
// object.
JsonValue operator[](const std::string& key) const;
bool HasKey(const std::string& key) const;

// Array access. `Size()` is 0 for anything that is not an array; `At()` returns an
// invalid value when the index is out of range.
std::size_t Size() const;
JsonValue At(std::size_t index) const;

private:
friend class core::JsonParser;

// `ParseJson` builds the root handle, so it needs the private constructor. Declared as a
// friend function rather than widening the constructor, so nothing else can mint a
// `JsonValue` from an impl.
friend Result<JsonValue> core::ParseJson(const std::string& text);

// `SerializeJson` resolves the document directly rather than rebuilding through the
// public surface, which would lose integer precision and could not represent an object.
friend std::string core::SerializeJson(const JsonValue& value);

struct Impl;

explicit JsonValue(std::shared_ptr<const Impl> impl);

// Shared, so a sub-value keeps its document alive and copying a handle is cheap. A
// `unique_ptr` would force a deep copy on every `operator[]`, which for a large response
// is the whole document.
std::shared_ptr<const Impl> m_impl;
};

} // namespace lootlocker
Loading
Loading