qmjsontype_double.cpp
Go to the documentation of this file.
1 //
2 // QtMark JSON Library
3 //
4 // Copyright (C) 2015 Assured Information Security, Inc.
5 // Author: Rian Quinn <quinnr@ainfosec.com>
6 // Author: Rodney Forbes <forbesr@ainfosec.com>
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 
22 // ============================================================================
23 // Includes
24 // ============================================================================
25 
26 #include <qmjsontype_double.h>
27 
28 // ============================================================================
29 // QMJsonType<double> Implementation
30 // ============================================================================
31 
32 QMPointer<QMJsonValue> convert(const QString &json, int32_t &index, int32_t start)
33 {
34  auto ok = false;
35  auto result = json.mid(start, index - start).toDouble(&ok);
36 
37  if(ok == false)
38  return QMPointer<QMJsonValue>(new QMJsonValue(0.0));
39 
40  return QMPointer<QMJsonValue>(new QMJsonValue(result));
41 }
42 
43 template <>
44 QMPointer<QMJsonValue> QM_JSON_EXPORT QMJsonType<double>::fromJson(const QString &json, int32_t &index)
45 {
46  auto start = index;
47 
48  while(1)
49  {
50  QMJsonValue::verifyIndex(json, index);
51 
52  switch(json.at(index).toLatin1())
53  {
54  case ']':
55  case '}':
56  case ':':
57  case ',':
58  case ' ':
59  case '\t':
60  case '\n':
61  case '\r':
62  return convert(json, index, start);
63 
64  default:
65  {
66  if(++index == json.length())
67  return convert(json, index, start);
68  }
69  };
70  }
71 
72  return QMPointer<QMJsonValue>(new QMJsonValue());
73 }
74 
75 template <>
77 {
78  (void)tab;
79  (void)sort;
80 
81  return QString::number(this->get(), 'g', 13);
82 }
83 
84 template <>
86 {
87  return true;
88 }
virtual QString toJson(int32_t tab, QMJsonSort sort)
Definition: qmjsontype.h:189
#define QM_JSON_EXPORT
Definition: qmjsontype.h:36
static QMPointer< QMJsonValue > fromJson(const QString &json, int32_t &index)
Definition: qmjsontype.h:198
virtual bool isBaseType(void)
Definition: qmjsontype.h:213
QMPointer< QMJsonValue > convert(const QString &json, int32_t &index, int32_t start)
QMJsonSort
Definition: qmjsontype.h:68