| 3 | namespace bdn |
| 4 | { |
| 5 | void AttributedString::fromJSON(const json &j) |
| 6 | { |
| 7 | if (j.count("html")) { |
| 8 | fromHTML(j.at("html")); |
| 9 | } else if (j.count("string")) { |
| 10 | fromString(j.at("string")); |
| 11 | } |
| 12 | |
| 13 | if (j.count("ranges")) { |
| 14 | auto ranges = j.at("ranges"); |
| 15 | |
| 16 | for (auto &range : ranges) { |
| 17 | size_t start = 0; |
| 18 | size_t length = -1; |
| 19 | |
| 20 | if (range.count("start")) { |
| 21 | start = range.at("start"); |
| 22 | } |
| 23 | if (range.count("length")) { |
| 24 | length = range.at("length"); |
| 25 | } |
| 26 | auto attributes = range.at("attributes"); |
| 27 | |
| 28 | std::map<std::string, std::any> attributeMap; |
| 29 | for (auto attribute : attributes.items()) { |
| 30 | attributeMap[attribute.key()] = attribute.value(); |
| 31 | } |
| 32 | if (!attributeMap.empty()) { |
| 33 | addAttributes(attributeMap, {start, length}); |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | json AttributedString::toJSON() const { return {{"html", toHTML()}}; } |
| 40 | } |