ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1020 lines
20 KiB

  1. #define CPPCMS_SOURCE
  2. #include "json.h"
  3. #include <sstream>
  4. #include <stdio.h>
  5. #include <typeinfo>
  6. #include <algorithm>
  7. #include <iostream>
  8. #include <locale>
  9. #include <stdexcept>
  10. #include <stack>
  11. #include "utf_iterator.h"
  12. #include "config.h"
  13. #ifdef CPPCMS_USE_EXTERNAL_BOOST
  14. # include <boost/variant.hpp>
  15. #else // Internal Boost
  16. # include <cppcms_boost/variant.hpp>
  17. namespace boost = cppcms_boost;
  18. #endif
  19. namespace cppcms {
  20. namespace json {
  21. bad_value_cast::bad_value_cast() : msg_("cppcms::json::bad_cast")
  22. {
  23. }
  24. bad_value_cast::bad_value_cast(std::string const &s) : msg_("cppcms::json::bad_cast: "+s )
  25. {
  26. }
  27. bad_value_cast::bad_value_cast(std::string const &s,json_type actual) :
  28. msg_("cppcms::json::bad_cast: ")
  29. {
  30. std::ostringstream msg;
  31. msg<<"errpr converting from "<<actual;
  32. msg_ +=msg.str();
  33. }
  34. bad_value_cast::bad_value_cast(std::string const &s,json_type expected, json_type actual) :
  35. msg_("cppcms::json::bad_cast: ")
  36. {
  37. std::ostringstream msg;
  38. msg<<"error converting from "<<actual<<" to "<<expected;
  39. msg_ +=msg.str();
  40. }
  41. bad_value_cast::~bad_value_cast() throw()
  42. {
  43. }
  44. const char* bad_value_cast::what() const throw()
  45. {
  46. return msg_.c_str();
  47. }
  48. typedef boost::variant<
  49. undefined,
  50. null,
  51. bool,
  52. double,
  53. std::string,
  54. json::object,
  55. json::array
  56. > variant_type;
  57. struct value::data {
  58. public:
  59. variant_type &value()
  60. {
  61. return value_;
  62. }
  63. variant_type const &value() const
  64. {
  65. return value_;
  66. }
  67. private:
  68. variant_type value_;
  69. };
  70. using namespace std;
  71. value::copyable::copyable() :
  72. d(new value::data())
  73. {
  74. }
  75. value::copyable::~copyable()
  76. {
  77. }
  78. value::copyable::copyable(copyable const &r) :
  79. d(r.d)
  80. {
  81. }
  82. value::copyable const &value::copyable::operator=(value::copyable const &r)
  83. {
  84. if(&r!=this)
  85. d=r.d;
  86. return *this;
  87. }
  88. template<typename T>
  89. T &get_variant_value(variant_type &v)
  90. {
  91. try {
  92. return boost::get<T>(v);
  93. }
  94. catch(boost::bad_get const &e) {
  95. throw bad_value_cast("",json_type(v.which()));
  96. }
  97. }
  98. template<typename T>
  99. T const &get_variant_value(variant_type const &v)
  100. {
  101. try {
  102. return boost::get<T>(v);
  103. }
  104. catch(boost::bad_get const &e) {
  105. throw bad_value_cast("",json_type(v.which()));
  106. }
  107. }
  108. double const &value::number() const
  109. {
  110. return get_variant_value<double>(d->value());
  111. }
  112. bool const &value::boolean() const
  113. {
  114. return get_variant_value<bool>(d->value());
  115. }
  116. std::string const &value::str() const
  117. {
  118. return get_variant_value<std::string>(d->value());
  119. }
  120. json::object const &value::object() const
  121. {
  122. return get_variant_value<json::object>(d->value());
  123. }
  124. json::array const &value::array() const
  125. {
  126. return get_variant_value<json::array>(d->value());
  127. }
  128. double &value::number()
  129. {
  130. return get_variant_value<double>(d->value());
  131. }
  132. bool &value::boolean()
  133. {
  134. return get_variant_value<bool>(d->value());
  135. }
  136. std::string &value::str()
  137. {
  138. return get_variant_value<std::string>(d->value());
  139. }
  140. json::object &value::object()
  141. {
  142. return get_variant_value<json::object>(d->value());
  143. }
  144. json::array &value::array()
  145. {
  146. return get_variant_value<json::array>(d->value());
  147. }
  148. bool value::is_undefined() const
  149. {
  150. return d->value().which()==json::is_undefined;
  151. }
  152. bool value::is_null() const
  153. {
  154. return d->value().which()==json::is_undefined;
  155. }
  156. void value::undefined()
  157. {
  158. d->value()=json::undefined();
  159. }
  160. void value::null()
  161. {
  162. d->value()=json::null();
  163. }
  164. void value::boolean(bool x)
  165. {
  166. d->value()=x;
  167. }
  168. void value::number(double x)
  169. {
  170. d->value()=x;
  171. }
  172. void value::str(std::string const &x)
  173. {
  174. d->value()=x;
  175. }
  176. void value::object(json::object const &x)
  177. {
  178. d->value()=x;
  179. }
  180. void value::array(json::array const &x)
  181. {
  182. d->value()=x;
  183. }
  184. json_type value::type() const
  185. {
  186. return json_type(d->value().which());
  187. }
  188. namespace {
  189. std::string escape(std::string const &input)
  190. {
  191. std::string result;
  192. result.reserve(input.size());
  193. result+='"';
  194. std::string::const_iterator i,end=input.end();
  195. for(i=input.begin();i!=end;i++) {
  196. switch(*i) {
  197. case 0x22:
  198. case 0x5C:
  199. result+='\\';
  200. result+=*i;
  201. break;
  202. case '\b': result+="\\b"; break;
  203. case '\f': result+="\\f"; break;
  204. case '\n': result+="\\n"; break;
  205. case '\r': result+="\\r"; break;
  206. case '\t': result+="\\t"; break;
  207. default:
  208. if(0x00<=*i && *i<=0x1F) {
  209. char buf[8];
  210. #ifdef HAVE_SNPRINTF
  211. snprintf(buf,sizeof(buf),"\\u%04X",*i);
  212. #else
  213. sprintf(buf,"\\u%04X",*i);
  214. #endif
  215. result+=buf;
  216. }
  217. else {
  218. result+=*i;
  219. }
  220. }
  221. }
  222. result+='"';
  223. return result;
  224. }
  225. void pad(std::ostream &out,int tb)
  226. {
  227. for(;tb > 0;tb--) out<<'\t';
  228. }
  229. void indent(std::ostream &out,char c,int &tabs)
  230. {
  231. if(tabs < 0) {
  232. out<<c;
  233. return;
  234. }
  235. switch(c) {
  236. case '{':
  237. case '[':
  238. out<<c<<'\n';
  239. tabs++;
  240. pad(out,tabs);
  241. break;
  242. case ',':
  243. out<<c<<'\n';
  244. pad(out,tabs);
  245. break;
  246. case ':':
  247. out<<" :\t";
  248. break;
  249. case '}':
  250. case ']':
  251. out<<'\n';
  252. tabs--;
  253. pad(out,tabs);
  254. out<<c<<'\n';
  255. pad(out,tabs);
  256. break;
  257. }
  258. }
  259. } // namespace anonymous
  260. void value::write(std::ostream &out,int tabs) const
  261. {
  262. std::locale original(out.getloc());
  263. out.imbue(std::locale("C"));
  264. try {
  265. write_value(out,tabs);
  266. }
  267. catch(...) {
  268. out.imbue(original);
  269. throw;
  270. }
  271. out.imbue(original);
  272. }
  273. void value::write_value(std::ostream &out,int tabs) const
  274. {
  275. switch(type()) {
  276. case json::is_undefined:
  277. throw bad_value_cast("Can't write undefined value to stream");
  278. case json::is_null:
  279. out<<"null";
  280. case json::is_number:
  281. out<<number();
  282. break;
  283. case json::is_string:
  284. out<<escape(str());
  285. break;
  286. case json::is_boolean:
  287. out<< (boolean() ? "true" : "false") ;
  288. break;
  289. case json::is_array:
  290. {
  291. json::array const &a=array();
  292. unsigned i;
  293. indent(out,'[',tabs);
  294. for(i=0;i<a.size();) {
  295. a[i].write_value(out,tabs);
  296. i++;
  297. if(i<a.size())
  298. indent(out,',',tabs);
  299. }
  300. indent(out,']',tabs);
  301. }
  302. break;
  303. case json::is_object:
  304. {
  305. json::object const &obj=object();
  306. object::const_iterator p,end;
  307. p=obj.begin();
  308. end=obj.end();
  309. indent(out,'{',tabs);
  310. while(p!=end) {
  311. out<<escape(p->first);
  312. indent(out,':',tabs);
  313. p->second.write_value(out,tabs);
  314. ++p;
  315. if(p!=end)
  316. indent(out,',',tabs);
  317. }
  318. indent(out,'}',tabs);
  319. }
  320. break;
  321. default:
  322. throw bad_value_cast("Unknown type found: internal error");
  323. }
  324. }
  325. void value::save(std::ostream &out,int how) const
  326. {
  327. int tabs=(how & readable) ? 0 : -1;
  328. write(out,tabs);
  329. }
  330. std::string value::save(int how) const
  331. {
  332. std::ostringstream ss;
  333. int tabs=(how & readable) ? 0 : -1;
  334. write(ss,tabs);
  335. return ss.str();
  336. }
  337. std::ostream &operator<<(std::ostream &out,value const &v)
  338. {
  339. v.save(out);
  340. return out;
  341. }
  342. bool value::operator==(value const &other) const
  343. {
  344. return d->value()==other.d->value();
  345. }
  346. bool value::operator!=(value const &other) const
  347. {
  348. return !(*this==other);
  349. }
  350. // returns empty if not found
  351. value const &value::find(std::string path) const
  352. {
  353. static value const empty;
  354. value const *ptr=this;
  355. size_t pos=0;
  356. size_t new_pos;
  357. do {
  358. new_pos=path.find('.',pos);
  359. std::string part=path.substr(pos,new_pos - pos);
  360. if(new_pos!=std::string::npos)
  361. new_pos++;
  362. if(part.empty())
  363. return empty;
  364. if(ptr->type()!=json::is_object)
  365. return empty;
  366. json::object const &obj=ptr->object();
  367. json::object::const_iterator p;
  368. if((p=obj.find(part))==obj.end())
  369. return empty;
  370. ptr=&p->second;
  371. pos=new_pos;
  372. } while(new_pos < path.size());
  373. return *ptr;
  374. }
  375. // throws if not found
  376. value const &value::at(std::string path) const
  377. {
  378. value const &v=find(path);
  379. if(v.is_undefined())
  380. throw bad_value_cast("Value not found at "+path );
  381. return v;
  382. }
  383. value &value::at(std::string path)
  384. {
  385. value *ptr=this;
  386. size_t pos=0;
  387. size_t new_pos;
  388. do {
  389. new_pos=path.find('.',pos);
  390. std::string part=path.substr(pos,new_pos - pos);
  391. if(new_pos!=std::string::npos)
  392. new_pos++;
  393. if(part.empty())
  394. throw bad_value_cast("Invalid path provided");
  395. if(ptr->type()!=json::is_object)
  396. throw bad_value_cast("",ptr->type(),json::is_object);
  397. json::object &obj=ptr->object();
  398. json::object::iterator p;
  399. if((p=obj.find(part))==obj.end())
  400. throw bad_value_cast("Member "+part+" not found");
  401. ptr=&p->second;
  402. pos=new_pos;
  403. } while(new_pos < path.size());
  404. return *ptr;
  405. }
  406. void value::at(std::string path,value const &v)
  407. {
  408. value *ptr=this;
  409. size_t pos=0;
  410. size_t new_pos;
  411. do {
  412. new_pos=path.find('.',pos);
  413. std::string part=path.substr(pos,new_pos - pos);
  414. if(new_pos!=std::string::npos)
  415. new_pos++;
  416. if(part.empty())
  417. throw bad_value_cast("Invalid path provided");
  418. if(ptr->type()!=json::is_object) {
  419. *ptr=json::object();
  420. }
  421. json::object &obj=ptr->object();
  422. json::object::iterator p;
  423. if((p=obj.find(part))==obj.end()) {
  424. ptr=&obj.insert(std::make_pair(part,json::value())).first->second;
  425. }
  426. else
  427. ptr=&p->second;
  428. pos=new_pos;
  429. } while(new_pos < path.size());
  430. *ptr=v;
  431. }
  432. value &value::operator[](std::string name)
  433. {
  434. if(type()!=json::is_object)
  435. set_value(json::object());
  436. json::object &self=object();
  437. json::object::iterator p=self.find(name);
  438. if(p==self.end())
  439. return self.insert(std::make_pair(name,value())).first->second;
  440. return p->second;
  441. }
  442. value const &value::operator[](std::string name) const
  443. {
  444. if(type()!=json::is_object)
  445. throw bad_value_cast("",type(),json::is_object);
  446. json::object const &self=object();
  447. json::object::const_iterator p=self.find(name);
  448. if(p==self.end())
  449. throw bad_value_cast("Member "+name+" not found");
  450. return p->second;
  451. }
  452. value &value::operator[](size_t n)
  453. {
  454. if(type()!=json::is_array)
  455. set_value(json::array());
  456. json::array &self=array();
  457. if(n>=self.size())
  458. self.resize(n+1);
  459. return self[n];
  460. }
  461. value const &value::operator[](size_t n) const
  462. {
  463. if(type()!=json::is_array)
  464. throw bad_value_cast("",type(),json::is_array);
  465. return array().at(n);
  466. }
  467. //#define DEBUG_PARSER
  468. namespace {
  469. enum {
  470. tock_eof = 255,
  471. tock_err,
  472. tock_str,
  473. tock_number,
  474. tock_true,
  475. tock_false,
  476. tock_null
  477. };
  478. struct tockenizer {
  479. public:
  480. tockenizer(std::istream &in) :
  481. real(0),
  482. line(1),
  483. is_(in),
  484. locale_(in.getloc())
  485. {
  486. is_.imbue(std::locale::classic());
  487. }
  488. ~tockenizer()
  489. {
  490. is_.imbue(locale_);
  491. }
  492. std::string str;
  493. double real;
  494. int line;
  495. #ifdef DEBUG_PARSER
  496. std::string write_tocken(int c)
  497. {
  498. std::ostringstream ss;
  499. if(c>=255) {
  500. static char const *name[] = {
  501. "eof",
  502. "err",
  503. "str",
  504. "number",
  505. "true",
  506. "false",
  507. "null"
  508. };
  509. ss<<name[c - 255];
  510. }
  511. else {
  512. ss<<char(c);
  513. }
  514. if(c==tock_str)
  515. ss<<" "<<str;
  516. else if(c==tock_number)
  517. ss<<" "<<real;
  518. return ss.str();
  519. }
  520. #endif
  521. int next()
  522. {
  523. for(;;) {
  524. char c;
  525. if(!is_.get(c))
  526. return tock_eof;
  527. switch(c) {
  528. case '[':
  529. case '{':
  530. case ':':
  531. case ',':
  532. case '}':
  533. case ']':
  534. return c;
  535. case ' ':
  536. case '\t':
  537. case '\r':
  538. break;
  539. case '\n':
  540. line++;
  541. break;
  542. case '"':
  543. is_.unget();
  544. if(parse_string())
  545. return tock_str;
  546. return tock_err;
  547. case 't':
  548. if(check("rue"))
  549. return tock_true;
  550. return tock_err;
  551. case 'n':
  552. if(check("ull"))
  553. return tock_null;
  554. return tock_err;
  555. case 'f':
  556. if(check("alse"))
  557. return tock_false;
  558. return tock_err;
  559. case '-':
  560. case '0':
  561. case '1':
  562. case '2':
  563. case '3':
  564. case '4':
  565. case '5':
  566. case '6':
  567. case '7':
  568. case '8':
  569. case '9':
  570. is_.unget();
  571. if(parse_number())
  572. return tock_number;
  573. return tock_err;
  574. case '/':
  575. if(check("/")) {
  576. while(is_.get(c) && c!='\n')
  577. ;
  578. if(c=='\n')
  579. break;
  580. return tock_eof;
  581. }
  582. return tock_err;
  583. default:
  584. return tock_err;
  585. }
  586. }
  587. }
  588. private:
  589. std::istream &is_;
  590. std::locale locale_;
  591. bool check(char const *s)
  592. {
  593. char c;
  594. while(*s && is_.get(c) && c==*s)
  595. s++;
  596. return *s==0;
  597. }
  598. bool parse_string()
  599. {
  600. char c;
  601. str.clear();
  602. if(!is_.get(c) || c!='"')
  603. return false;
  604. bool second_surragate_expected=false;
  605. uint16_t first_surragate = 0;
  606. for(;;) {
  607. if(!is_.get(c))
  608. return false;
  609. if(second_surragate_expected && c!='\\')
  610. return false;
  611. if(0<= c && c <= 0x1F)
  612. return false;
  613. if(c=='"')
  614. break;
  615. if(c=='\\') {
  616. if(!is_.get(c))
  617. return false;
  618. if(second_surragate_expected && c!='u')
  619. return false;
  620. switch(c) {
  621. case '"':
  622. case '\\':
  623. case '/':
  624. str+=char(c);
  625. break;
  626. case 'b': str+='\b'; break;
  627. case 'f': str+='\f'; break;
  628. case 'n': str+='\n'; break;
  629. case 'r': str+='\r'; break;
  630. case 't': str+='\t'; break;
  631. case 'u':
  632. {
  633. uint16_t x;
  634. if(!read_4_digits(x))
  635. return false;
  636. if(second_surragate_expected) {
  637. if(!utf16::is_second_surrogate(x))
  638. return false;
  639. append(utf16::combine_surrogate(first_surragate,x));
  640. }
  641. else if(utf16::is_first_surrogate(x)) {
  642. second_surragate_expected=true;
  643. first_surragate=x;
  644. }
  645. else {
  646. append(x);
  647. }
  648. }
  649. break;
  650. default:
  651. return false;
  652. }
  653. }
  654. else {
  655. str+=char(c);
  656. }
  657. }
  658. if(!utf8::validate(str.begin(),str.end()))
  659. return false;
  660. return true;
  661. }
  662. bool read_4_digits(uint16_t &x)
  663. {
  664. char buf[5]={0};
  665. if(!is_.get(buf,5))
  666. return false;
  667. for(unsigned i=0;i<4;i++) {
  668. char c=buf[i];
  669. if( ('0'<= c && c<='9')
  670. || ('A'<= c && c<='F')
  671. || ('a'<= c && c<='f') )
  672. {
  673. continue;
  674. }
  675. return false;
  676. }
  677. unsigned v;
  678. sscanf(buf,"%x",&v);
  679. x=v;
  680. return true;
  681. }
  682. void append(uint32_t x)
  683. {
  684. utf8::seq s=utf8::encode(x);
  685. str.append(s.c,s.len);
  686. }
  687. bool parse_number()
  688. {
  689. is_ >> real;
  690. return is_;
  691. }
  692. };
  693. typedef enum {
  694. st_init = 0,
  695. st_object_or_array_expected = 0 ,
  696. st_object_key_or_close_expected,
  697. st_object_colon_expected,
  698. st_object_value_expected,
  699. st_object_close_or_comma_expected,
  700. st_array_value_or_close_expected,
  701. st_array_close_or_comma_expected,
  702. st_error,
  703. st_done
  704. } state_type;
  705. #ifdef DEBUG_PARSER
  706. std::ostream &operator<<(std::ostream &out,state_type t)
  707. {
  708. static char const *names[] = {
  709. "st_object_or_array_expected",
  710. "st_object_key_or_close_expected",
  711. "st_object_colon_expected",
  712. "st_object_value_expected",
  713. "st_object_close_or_comma_expected",
  714. "st_array_value_or_close_expected",
  715. "st_array_close_or_comma_expected",
  716. "st_error",
  717. "st_done"
  718. };
  719. out<<names[t];
  720. return out;
  721. }
  722. #endif
  723. bool parse_stream(std::istream &in,value &out,bool force_eof,int &error_at_line)
  724. {
  725. tockenizer tock(in);
  726. value result;
  727. state_type state=st_init;
  728. std::string key;
  729. std::stack<std::pair<state_type,value *> > stack;
  730. stack.push(std::make_pair(st_done,&result));
  731. while(!stack.empty() && state !=st_error && state!=st_done) {
  732. int c=tock.next();
  733. #ifdef DEBUG_PARSER
  734. std::cout<<state<<" "<<tock.write_tocken(c)<<std::endl;
  735. #endif
  736. switch(state) {
  737. case st_object_or_array_expected:
  738. if(c=='[') {
  739. *stack.top().second=json::array();
  740. state=st_array_value_or_close_expected;
  741. }
  742. else if(c=='{') {
  743. *stack.top().second=json::object();
  744. state=st_object_key_or_close_expected;
  745. }
  746. else
  747. state = st_error;
  748. break;
  749. case st_object_key_or_close_expected:
  750. if(c=='}') {
  751. state=stack.top().first;
  752. stack.pop();
  753. }
  754. else if (c==tock_str) {
  755. key=tock.str;
  756. state = st_object_colon_expected;
  757. }
  758. else
  759. state = st_error;
  760. break;
  761. case st_object_colon_expected:
  762. if(c!=':')
  763. state=st_error;
  764. else
  765. state=st_object_value_expected;
  766. break;
  767. case st_object_value_expected:
  768. {
  769. json::object &obj = stack.top().second->object();
  770. std::pair<json::object::iterator,bool> res=
  771. obj.insert(std::make_pair(key,json::value()));
  772. if(res.second==false) {
  773. state=st_error;
  774. break;
  775. }
  776. json::value &val=res.first->second;
  777. if(c==tock_str) {
  778. val=tock.str;
  779. state=st_object_close_or_comma_expected;
  780. }
  781. else if(c==tock_true) {
  782. val=true;
  783. state=st_object_close_or_comma_expected;
  784. }
  785. else if(c==tock_false) {
  786. val=false;
  787. state=st_object_close_or_comma_expected;
  788. }
  789. else if(c==tock_null) {
  790. val=null();
  791. state=st_object_close_or_comma_expected;
  792. }
  793. else if(c==tock_number) {
  794. val=tock.real;
  795. state=st_object_close_or_comma_expected;
  796. }
  797. else if(c=='[') {
  798. val=json::array();
  799. state=st_array_value_or_close_expected;
  800. stack.push(std::make_pair(st_object_close_or_comma_expected,&val));
  801. }
  802. else if(c=='{') {
  803. val=json::object();
  804. state=st_object_key_or_close_expected;
  805. stack.push(std::make_pair(st_object_close_or_comma_expected,&val));
  806. }
  807. else
  808. state=st_error;
  809. }
  810. break;
  811. case st_object_close_or_comma_expected:
  812. if(c==',')
  813. state=st_object_key_or_close_expected;
  814. else if(c=='}') {
  815. state=stack.top().first;
  816. stack.pop();
  817. }
  818. else
  819. state=st_error;
  820. break;
  821. case st_array_value_or_close_expected:
  822. {
  823. if(c==']') {
  824. state=stack.top().first;
  825. stack.pop();
  826. break;
  827. }
  828. json::array &ar = stack.top().second->array();
  829. ar.push_back(json::value());
  830. json::value &val=ar.back();
  831. if(c==tock_str) {
  832. val=tock.str;
  833. state=st_array_close_or_comma_expected;
  834. }
  835. else if(c==tock_true) {
  836. val=true;
  837. state=st_array_close_or_comma_expected;
  838. }
  839. else if(c==tock_false) {
  840. val=false;
  841. state=st_array_close_or_comma_expected;
  842. }
  843. else if(c==tock_null) {
  844. val=null();
  845. state=st_array_close_or_comma_expected;
  846. }
  847. else if(c==tock_number) {
  848. val=tock.real;
  849. state=st_array_close_or_comma_expected;
  850. }
  851. else if(c=='[') {
  852. val=json::array();
  853. state=st_array_value_or_close_expected;
  854. stack.push(std::make_pair(st_array_close_or_comma_expected,&val));
  855. }
  856. else if(c=='{') {
  857. val=json::object();
  858. state=st_object_key_or_close_expected;
  859. stack.push(std::make_pair(st_array_close_or_comma_expected,&val));
  860. }
  861. else
  862. state=st_error;
  863. break;
  864. }
  865. case st_array_close_or_comma_expected:
  866. if(c==']') {
  867. state=stack.top().first;
  868. stack.pop();
  869. }
  870. else if(c==',')
  871. state=st_array_value_or_close_expected;
  872. else
  873. state=st_error;
  874. break;
  875. case st_done:
  876. case st_error:
  877. break;
  878. };
  879. }
  880. if(state==st_done) {
  881. if(force_eof) {
  882. if(tock.next()!=tock_eof) {
  883. error_at_line=tock.line;
  884. return false;
  885. }
  886. }
  887. out.swap(result);
  888. return true;
  889. }
  890. error_at_line=tock.line;
  891. return false;
  892. }
  893. } // anonymous
  894. bool value::load(std::istream &in,bool full,int *line_number)
  895. {
  896. int err_line;
  897. value v;
  898. if(!parse_stream(in,*this,full,err_line)) {
  899. if(line_number)
  900. *line_number=err_line;
  901. return false;
  902. }
  903. return true;
  904. }
  905. std::istream &operator>>(std::istream &in,value &v)
  906. {
  907. int line_no;
  908. if(!parse_stream(in,v,false,line_no))
  909. in.setstate( std::istream::failbit );
  910. return in;
  911. }
  912. std::ostream &operator<<(std::ostream &out,json_type t)
  913. {
  914. switch(t) {
  915. case is_undefined: out<<"undefined"; break;
  916. case is_null: out<<"null"; break;
  917. case is_boolean: out<<"boolean"; break;
  918. case is_number: out<<"number"; break;
  919. case is_string: out<<"string"; break;
  920. case is_object: out<<"object"; break;
  921. case is_array: out<<"array"; break;
  922. default:
  923. out<<"Illegal";
  924. }
  925. return out;
  926. }
  927. } // json
  928. } // cppcms