Created
January 4, 2025 02:33
-
-
Save erming/3385ae4d9ec5cf9e5034cf66856645de to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| void toJSON() { | |
| std::string json; | |
| int prependComma = 0; | |
| int rows = 0; | |
| auto _beginBlock = [&] () { prependComma = 1; rows = 0; }; | |
| auto _endBlock = [&] () { prependComma = 0; rows = 0; }; | |
| auto append = [&] (std::string a, std::string b = "") { | |
| if (prependComma && rows > 0) { | |
| json.append(","); | |
| } | |
| if (!a.empty()) json.append(a); | |
| if (!b.empty()) json.append(b); | |
| rows++; | |
| }; | |
| auto quoted = [] (std::string s) { | |
| return std::string("\"") | |
| .append(s) | |
| .append("\""); | |
| }; | |
| static int id = 0; | |
| static char temp[6]; | |
| json.reserve(768); | |
| append("{"); | |
| append( "\"version\":", quoted("1.0")); | |
| append(",\"msgType\":", quoted("")); | |
| append(",\"hwid\":", quoted("")); | |
| append(",\"imei\":", quoted("")); | |
| append(",\"imsi\":", quoted("")); | |
| append(",\"internal\":{"); | |
| append( "\"id\":", quoted("")); | |
| append(",\"time\":", quoted("")); | |
| append(",\"temp\":", quoted("")); | |
| append("}"); | |
| append(",\"settings\":{"); | |
| append( "\"autoClose\":", quoted("")); | |
| append(",\"ping\":", quoted("")); | |
| append(",\"mode\":{"); | |
| append( "\"fire\":", quoted("")); | |
| append(",\"ups\":", quoted("")); | |
| append("}"); | |
| append("}"); | |
| append(",\"versions\":{"); | |
| append( "\"hw\":", quoted("")); | |
| append(",\"sw\":", quoted("")); | |
| append("}"); | |
| append("}"); | |
| std::cout << json << std::endl; | |
| } | |
| int main() { | |
| toJSON(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment