On this page, we’ll see how we can create a custom converter so you can directly insert or retrieve instances of IPAddress like so:

// Insert into a JsonDocument
doc["ip"] = IPAddress(192, 168, 0, 1);

// Retrieve from a JsonDocument
IPAddress ip = doc["ip"];

In this scenario, the JSON document looks like this:

{"ip":"192.168.0.1"}

Converting IPAddress to JSON

Inserting an IPAddress into a JsonDocument works out-of-the-box because IPAddress implements the Printable interface (and ArduinoJson has supported this interface since version 6.18.0).

Therefore, you don’t need to write any code.

Converting JSON to IPAddress

You must declare the following function:

void convertFromJson(JsonVariantConst src, IPAddress& dst) {
  dst.fromString(src.as<const char*>());
}

As you can see, this function calls IPAddress.fromString() to parse the string "192.168.0.1".

convertFromJson() must have this exact signature. Don’t change its name or parameter types; otherwise, ArduinoJson won’t find it.

Global warming stripes by Professor Ed Hawkins (University of Reading)