| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/json/qjsonwriter.cpp | 
| Switch to Source code | Preprocessed file | 
| Line | Source | Count | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | - | |||||||||||||||||||
| 2 | - | |||||||||||||||||||
| 3 | - | |||||||||||||||||||
| 4 | using namespace QJsonPrivate; | - | ||||||||||||||||||
| 5 | - | |||||||||||||||||||
| 6 | static void objectContentToJson(const QJsonPrivate::Object *o, QByteArray &json, int indent, bool compact); | - | ||||||||||||||||||
| 7 | static void arrayContentToJson(const QJsonPrivate::Array *a, QByteArray &json, int indent, bool compact); | - | ||||||||||||||||||
| 8 | - | |||||||||||||||||||
| 9 | static inline uchar hexdig(uint u) | - | ||||||||||||||||||
| 10 | { | - | ||||||||||||||||||
| 11 |     return never executed:   (u < 0xareturn (u < 0xa ? '0' + u : 'a' + u - 0xa);
 never executed:  return (u < 0xa ? '0' + u : 'a' + u - 0xa); | 0 | ||||||||||||||||||
| 12 | } | - | ||||||||||||||||||
| 13 | - | |||||||||||||||||||
| 14 | static QByteArray escapedString(const QString &s) | - | ||||||||||||||||||
| 15 | { | - | ||||||||||||||||||
| 16 | const uchar replacement = '?'; | - | ||||||||||||||||||
| 17 | QByteArray ba(s.length(), Qt::Uninitialized); | - | ||||||||||||||||||
| 18 | - | |||||||||||||||||||
| 19 | uchar *cursor = reinterpret_cast<uchar *>(const_cast<char *>(ba.constData())); | - | ||||||||||||||||||
| 20 | const uchar *ba_end = cursor + ba.length(); | - | ||||||||||||||||||
| 21 | const ushort *src = reinterpret_cast<const ushort *>(s.constBegin()); | - | ||||||||||||||||||
| 22 | const ushort *const end = reinterpret_cast<const ushort *>(s.constEnd()); | - | ||||||||||||||||||
| 23 | - | |||||||||||||||||||
| 24 |     while (src != end
  | 0 | ||||||||||||||||||
| 25 |         if (cursor >= ba_end - 6
  | 0 | ||||||||||||||||||
| 26 | - | |||||||||||||||||||
| 27 | int pos = cursor - (const uchar *)ba.constData(); | - | ||||||||||||||||||
| 28 | ba.resize(ba.size()*2); | - | ||||||||||||||||||
| 29 | cursor = (uchar *)ba.data() + pos; | - | ||||||||||||||||||
| 30 | ba_end = (const uchar *)ba.constData() + ba.length(); | - | ||||||||||||||||||
| 31 |         } never executed:  end of block | 0 | ||||||||||||||||||
| 32 | - | |||||||||||||||||||
| 33 | uint u = *src++; | - | ||||||||||||||||||
| 34 |         if (u < 0x80
  | 0 | ||||||||||||||||||
| 35 |             if (u < 0x20
 
 
  | 0 | ||||||||||||||||||
| 36 | *cursor++ = '\\'; | - | ||||||||||||||||||
| 37 | switch (u) { | - | ||||||||||||||||||
| 38 |                 case never executed:   0x22:case 0x22:never executed:  case 0x22: | 0 | ||||||||||||||||||
| 39 | *cursor++ = '"'; | - | ||||||||||||||||||
| 40 |                     break; never executed:  break; | 0 | ||||||||||||||||||
| 41 |                 case never executed:   0x5c:case 0x5c:never executed:  case 0x5c: | 0 | ||||||||||||||||||
| 42 | *cursor++ = '\\'; | - | ||||||||||||||||||
| 43 |                     break; never executed:  break; | 0 | ||||||||||||||||||
| 44 |                 case never executed:   0x8:case 0x8:never executed:  case 0x8: | 0 | ||||||||||||||||||
| 45 | *cursor++ = 'b'; | - | ||||||||||||||||||
| 46 |                     break; never executed:  break; | 0 | ||||||||||||||||||
| 47 |                 case never executed:   0xc:case 0xc:never executed:  case 0xc: | 0 | ||||||||||||||||||
| 48 | *cursor++ = 'f'; | - | ||||||||||||||||||
| 49 |                     break; never executed:  break; | 0 | ||||||||||||||||||
| 50 |                 case never executed:   0xa:case 0xa:never executed:  case 0xa: | 0 | ||||||||||||||||||
| 51 | *cursor++ = 'n'; | - | ||||||||||||||||||
| 52 |                     break; never executed:  break; | 0 | ||||||||||||||||||
| 53 |                 case never executed:   0xd:case 0xd:never executed:  case 0xd: | 0 | ||||||||||||||||||
| 54 | *cursor++ = 'r'; | - | ||||||||||||||||||
| 55 |                     break; never executed:  break; | 0 | ||||||||||||||||||
| 56 |                 case never executed:   0x9:case 0x9:never executed:  case 0x9: | 0 | ||||||||||||||||||
| 57 | *cursor++ = 't'; | - | ||||||||||||||||||
| 58 |                     break; never executed:  break; | 0 | ||||||||||||||||||
| 59 |                 default never executed:  :default:never executed:  default: | 0 | ||||||||||||||||||
| 60 | *cursor++ = 'u'; | - | ||||||||||||||||||
| 61 | *cursor++ = '0'; | - | ||||||||||||||||||
| 62 | *cursor++ = '0'; | - | ||||||||||||||||||
| 63 | *cursor++ = hexdig(u>>4); | - | ||||||||||||||||||
| 64 | *cursor++ = hexdig(u & 0xf); | - | ||||||||||||||||||
| 65 |                } never executed:  end of block | 0 | ||||||||||||||||||
| 66 | } else { | - | ||||||||||||||||||
| 67 | *cursor++ = (uchar)u; | - | ||||||||||||||||||
| 68 |             } never executed:  end of block | 0 | ||||||||||||||||||
| 69 | } else { | - | ||||||||||||||||||
| 70 |             if (QUtf8Functions::toUtf8<QUtf8BaseTraits>(u, cursor, src, end) < 0
  | 0 | ||||||||||||||||||
| 71 |                 * never executed:  cursor++ = replacement;*cursor++ = replacement;never executed:  *cursor++ = replacement; | 0 | ||||||||||||||||||
| 72 |         } never executed:  end of block | 0 | ||||||||||||||||||
| 73 | } | - | ||||||||||||||||||
| 74 | - | |||||||||||||||||||
| 75 | ba.resize(cursor - (const uchar *)ba.constData()); | - | ||||||||||||||||||
| 76 |     return never executed:   ba;return ba;never executed:  return ba; | 0 | ||||||||||||||||||
| 77 | } | - | ||||||||||||||||||
| 78 | - | |||||||||||||||||||
| 79 | static void valueToJson(const QJsonPrivate::Base *b, const QJsonPrivate::Value &v, QByteArray &json, int indent, bool compact) | - | ||||||||||||||||||
| 80 | { | - | ||||||||||||||||||
| 81 | QJsonValue::Type type = (QJsonValue::Type)(uint)v.type; | - | ||||||||||||||||||
| 82 | switch (type) { | - | ||||||||||||||||||
| 83 |     case never executed:   QJsonValue::Bool:case QJsonValue::Bool:never executed:  case QJsonValue::Bool: | 0 | ||||||||||||||||||
| 84 |         json += v.toBoolean()
  | 0 | ||||||||||||||||||
| 85 |         break; never executed:  break; | 0 | ||||||||||||||||||
| 86 |     case never executed:   QJsonValue::Double:case QJsonValue::Double:never executed:   {case QJsonValue::Double: | 0 | ||||||||||||||||||
| 87 | const double d = v.toDouble(b); | - | ||||||||||||||||||
| 88 |         if (qIsFinite(d)
  | 0 | ||||||||||||||||||
| 89 |             json += QByteArray::number(d, 'g', std::numeric_limits<double>::digits10 + 2); never executed:  json += QByteArray::number(d, 'g', std::numeric_limits<double>::digits10 + 2); | 0 | ||||||||||||||||||
| 90 | else | - | ||||||||||||||||||
| 91 |             json += "null"; never executed:  json += "null"; | 0 | ||||||||||||||||||
| 92 |         break; never executed:  break; | 0 | ||||||||||||||||||
| 93 | } | - | ||||||||||||||||||
| 94 |     case never executed:   QJsonValue::String:case QJsonValue::String:never executed:  case QJsonValue::String: | 0 | ||||||||||||||||||
| 95 | json += '"'; | - | ||||||||||||||||||
| 96 | json += escapedString(v.toString(b)); | - | ||||||||||||||||||
| 97 | json += '"'; | - | ||||||||||||||||||
| 98 |         break; never executed:  break; | 0 | ||||||||||||||||||
| 99 |     case never executed:   QJsonValue::Array:case QJsonValue::Array:never executed:  case QJsonValue::Array: | 0 | ||||||||||||||||||
| 100 |         json += compact
  | 0 | ||||||||||||||||||
| 101 | arrayContentToJson(static_cast<QJsonPrivate::Array *>(v.base(b)), json, indent + (compact ? 0 : 1), compact); | - | ||||||||||||||||||
| 102 | json += QByteArray(4*indent, ' '); | - | ||||||||||||||||||
| 103 | json += ']'; | - | ||||||||||||||||||
| 104 |         break; never executed:  break; | 0 | ||||||||||||||||||
| 105 |     case never executed:   QJsonValue::Object:case QJsonValue::Object:never executed:  case QJsonValue::Object: | 0 | ||||||||||||||||||
| 106 |         json += compact
  | 0 | ||||||||||||||||||
| 107 | objectContentToJson(static_cast<QJsonPrivate::Object *>(v.base(b)), json, indent + (compact ? 0 : 1), compact); | - | ||||||||||||||||||
| 108 | json += QByteArray(4*indent, ' '); | - | ||||||||||||||||||
| 109 | json += '}'; | - | ||||||||||||||||||
| 110 |         break; never executed:  break; | 0 | ||||||||||||||||||
| 111 |     case never executed:   QJsonValue::Null:case QJsonValue::Null:never executed:  case QJsonValue::Null: | 0 | ||||||||||||||||||
| 112 |     default never executed:  :default:never executed:  default: | 0 | ||||||||||||||||||
| 113 | json += "null"; | - | ||||||||||||||||||
| 114 |     } never executed:  end of block | 0 | ||||||||||||||||||
| 115 | } | - | ||||||||||||||||||
| 116 | - | |||||||||||||||||||
| 117 | static void arrayContentToJson(const QJsonPrivate::Array *a, QByteArray &json, int indent, bool compact) | - | ||||||||||||||||||
| 118 | { | - | ||||||||||||||||||
| 119 |     if (!a
 
  | 0 | ||||||||||||||||||
| 120 |         return; never executed:  return; | 0 | ||||||||||||||||||
| 121 | - | |||||||||||||||||||
| 122 | QByteArray indentString(4*indent, ' '); | - | ||||||||||||||||||
| 123 | - | |||||||||||||||||||
| 124 | uint i = 0; | - | ||||||||||||||||||
| 125 | while (1) { | - | ||||||||||||||||||
| 126 | json += indentString; | - | ||||||||||||||||||
| 127 | valueToJson(a, a->at(i), json, indent, compact); | - | ||||||||||||||||||
| 128 | - | |||||||||||||||||||
| 129 |         if (++
 
  | 0 | ||||||||||||||||||
| 130 |             if (!compact
  | 0 | ||||||||||||||||||
| 131 |                 json += '\n'; never executed:  json += '\n'; | 0 | ||||||||||||||||||
| 132 |             break; never executed:  break; | 0 | ||||||||||||||||||
| 133 | } | - | ||||||||||||||||||
| 134 | - | |||||||||||||||||||
| 135 |         json += compact
  | 0 | ||||||||||||||||||
| 136 |     } never executed:  end of block | 0 | ||||||||||||||||||
| 137 | } never executed:  end of block | 0 | ||||||||||||||||||
| 138 | - | |||||||||||||||||||
| 139 | - | |||||||||||||||||||
| 140 | static void objectContentToJson(const QJsonPrivate::Object *o, QByteArray &json, int indent, bool compact) | - | ||||||||||||||||||
| 141 | { | - | ||||||||||||||||||
| 142 |     if (!o
 
  | 0 | ||||||||||||||||||
| 143 |         return; never executed:  return; | 0 | ||||||||||||||||||
| 144 | - | |||||||||||||||||||
| 145 | QByteArray indentString(4*indent, ' '); | - | ||||||||||||||||||
| 146 | - | |||||||||||||||||||
| 147 | uint i = 0; | - | ||||||||||||||||||
| 148 | while (1) { | - | ||||||||||||||||||
| 149 | QJsonPrivate::Entry *e = o->entryAt(i); | - | ||||||||||||||||||
| 150 | json += indentString; | - | ||||||||||||||||||
| 151 | json += '"'; | - | ||||||||||||||||||
| 152 | json += escapedString(e->key()); | - | ||||||||||||||||||
| 153 |         json += compact
  | 0 | ||||||||||||||||||
| 154 | valueToJson(o, e->value, json, indent, compact); | - | ||||||||||||||||||
| 155 | - | |||||||||||||||||||
| 156 |         if (++
 
  | 0 | ||||||||||||||||||
| 157 |             if (!compact
  | 0 | ||||||||||||||||||
| 158 |                 json += '\n'; never executed:  json += '\n'; | 0 | ||||||||||||||||||
| 159 |             break; never executed:  break; | 0 | ||||||||||||||||||
| 160 | } | - | ||||||||||||||||||
| 161 | - | |||||||||||||||||||
| 162 |         json += compact
  | 0 | ||||||||||||||||||
| 163 |     } never executed:  end of block | 0 | ||||||||||||||||||
| 164 | } never executed:  end of block | 0 | ||||||||||||||||||
| 165 | - | |||||||||||||||||||
| 166 | void Writer::objectToJson(const QJsonPrivate::Object *o, QByteArray &json, int indent, bool compact) | - | ||||||||||||||||||
| 167 | { | - | ||||||||||||||||||
| 168 | json.reserve(json.size() + (o ? (int)o->size : 16)); | - | ||||||||||||||||||
| 169 |     json += compact
  | 0 | ||||||||||||||||||
| 170 | objectContentToJson(o, json, indent + (compact ? 0 : 1), compact); | - | ||||||||||||||||||
| 171 | json += QByteArray(4*indent, ' '); | - | ||||||||||||||||||
| 172 |     json += compact
  | 0 | ||||||||||||||||||
| 173 | } never executed:  end of block | 0 | ||||||||||||||||||
| 174 | - | |||||||||||||||||||
| 175 | void Writer::arrayToJson(const QJsonPrivate::Array *a, QByteArray &json, int indent, bool compact) | - | ||||||||||||||||||
| 176 | { | - | ||||||||||||||||||
| 177 | json.reserve(json.size() + (a ? (int)a->size : 16)); | - | ||||||||||||||||||
| 178 |     json += compact
  | 0 | ||||||||||||||||||
| 179 | arrayContentToJson(a, json, indent + (compact ? 0 : 1), compact); | - | ||||||||||||||||||
| 180 | json += QByteArray(4*indent, ' '); | - | ||||||||||||||||||
| 181 |     json += compact
  | 0 | ||||||||||||||||||
| 182 | } never executed:  end of block | 0 | ||||||||||||||||||
| 183 | - | |||||||||||||||||||
| 184 | - | |||||||||||||||||||
| Switch to Source code | Preprocessed file |