OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "GrAuditTrail.h" | 8 #include "GrAuditTrail.h" |
9 | 9 |
| 10 template <class T> |
| 11 static void jsonify_tarray(SkString* json, const SkTArray<T>& array) { |
| 12 for (int i = 0; i < array.count(); i++) { |
| 13 json->append(array[i].toJson()); |
| 14 if (i < array.count() - 1) { |
| 15 json->append(","); |
| 16 } |
| 17 } |
| 18 } |
| 19 |
| 20 // This will pretty print a very small subset of json |
| 21 // The parsing rules are straightforward, aside from the fact that we do not wan
t an extra newline |
| 22 // before ',' and after '}', so we have a comma exception rule. |
| 23 class PrettyPrintJson { |
| 24 public: |
| 25 SkString prettify(const SkString& json) { |
| 26 fPrettyJson.reset(); |
| 27 fTabCount = 0; |
| 28 fFreshLine = false; |
| 29 fCommaException = false; |
| 30 for (size_t i = 0; i < json.size(); i++) { |
| 31 if ('[' == json[i] || '{' == json[i]) { |
| 32 this->newline(); |
| 33 this->appendChar(json[i]); |
| 34 fTabCount++; |
| 35 this->newline(); |
| 36 } else if (']' == json[i] || '}' == json[i]) { |
| 37 fTabCount--; |
| 38 this->newline(); |
| 39 this->appendChar(json[i]); |
| 40 fCommaException = true; |
| 41 } else if (',' == json[i]) { |
| 42 this->appendChar(json[i]); |
| 43 this->newline(); |
| 44 } else { |
| 45 this->appendChar(json[i]); |
| 46 } |
| 47 } |
| 48 return fPrettyJson; |
| 49 } |
| 50 private: |
| 51 void appendChar(char appendee) { |
| 52 if (fCommaException && ',' != appendee) { |
| 53 this->newline(); |
| 54 } |
| 55 this->tab(); |
| 56 fPrettyJson += appendee; |
| 57 fFreshLine = false; |
| 58 fCommaException = false; |
| 59 } |
| 60 |
| 61 void tab() { |
| 62 if (fFreshLine) { |
| 63 for (int i = 0; i < fTabCount; i++) { |
| 64 fPrettyJson += '\t'; |
| 65 } |
| 66 } |
| 67 } |
| 68 |
| 69 void newline() { |
| 70 if (!fFreshLine) { |
| 71 fFreshLine = true; |
| 72 fPrettyJson += '\n'; |
| 73 } |
| 74 } |
| 75 |
| 76 SkString fPrettyJson; |
| 77 int fTabCount; |
| 78 bool fFreshLine; |
| 79 bool fCommaException; |
| 80 }; |
| 81 |
| 82 static SkString pretty_print_json(SkString json) { |
| 83 class PrettyPrintJson prettyPrintJson; |
| 84 return prettyPrintJson.prettify(json); |
| 85 } |
| 86 |
10 SkString GrAuditTrail::toJson() const { | 87 SkString GrAuditTrail::toJson() const { |
11 SkString json; | 88 SkString json; |
12 json.append("{\n"); | 89 json.append("{"); |
13 json.append("\"Ops\": [\n"); | 90 json.append("\"Ops\": ["); |
14 for (int i = 0; i < fOps.count(); i++) { | 91 jsonify_tarray(&json, fOps); |
15 json.append(fOps[i].toJson()); | 92 json.append("]"); |
16 if (i < fOps.count() - 1) { | 93 json.append("}"); |
17 json.append(",\n"); | 94 |
18 } | 95 // TODO if this becomes a performance issue we should make pretty print conf
igurable |
19 } | 96 return pretty_print_json(json); |
20 json.append("]\n"); | |
21 json.append("}\n"); | |
22 return json; | |
23 } | 97 } |
24 | 98 |
25 SkString GrAuditTrail::Op::toJson() const { | 99 SkString GrAuditTrail::Op::toJson() const { |
26 SkString json; | 100 SkString json; |
27 json.append("{\n"); | 101 json.append("{"); |
28 json.appendf("\"Name\": \"%s\"\n", fName.c_str()); | 102 json.appendf("\"Name\": \"%s\",", fName.c_str()); |
29 json.append("}\n"); | 103 json.append("\"Batches\": ["); |
| 104 jsonify_tarray(&json, fBatches); |
| 105 json.append("]"); |
| 106 json.append("}"); |
30 return json; | 107 return json; |
31 } | 108 } |
32 | 109 |
| 110 SkString GrAuditTrail::Op::Batch::toJson() const { |
| 111 SkString json; |
| 112 json.append("{"); |
| 113 json.appendf("\"Name\": \"%s\",", fName.c_str()); |
| 114 json.append("\"Bounds\": {"); |
| 115 json.appendf("\"Left\": %f,", fBounds.fLeft); |
| 116 json.appendf("\"Right\": %f,", fBounds.fRight); |
| 117 json.appendf("\"Top\": %f,", fBounds.fTop); |
| 118 json.appendf("\"Bottom\": %f", fBounds.fBottom); |
| 119 json.append("}"); |
| 120 json.append("}"); |
| 121 return json; |
| 122 } |
OLD | NEW |