Skip to content

Commit 5e6ef1c

Browse files
committed
fixed GH pocoproject#99: JSON::Query an JSON::Object
- fixed GH pocoproject#99: JSON::Query an JSON::Object - swapped order of AnyCast(const Any&) and AnyCast(Any&) definitions
1 parent 1d2a95e commit 5e6ef1c

9 files changed

Lines changed: 440 additions & 41 deletions

File tree

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ Release 1.5.2 (2013-06-xx)
7676
- fixed GH #185: Poco::NumberFormatter::format(double value, int precision)
7777
ignore precision == 0
7878
- fixed GH #138: FreeBSD JSON tests fail
79+
- fixed GH #99: JSON::Query an JSON::Object
7980

8081

8182
Release 1.5.1 (2013-01-11)

Foundation/include/Poco/Any.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -534,38 +534,38 @@ const ValueType* AnyCast(const Any* operand)
534534

535535

536536
template <typename ValueType>
537-
ValueType AnyCast(const Any& operand)
538-
/// AnyCast operator used to extract a copy of the ValueType from an const Any&.
537+
ValueType AnyCast(Any& operand)
538+
/// AnyCast operator used to extract a copy of the ValueType from an Any&.
539539
///
540540
/// Example Usage:
541541
/// MyType tmp = AnyCast<MyType>(anAny).
542542
/// Will throw a BadCastException if the cast fails.
543-
/// Dont use an AnyCast in combination with references, i.e. MyType& tmp = ... or const MyType& = ...
543+
/// Dont use an AnyCast in combination with references, i.e. MyType& tmp = ... or const MyType& tmp = ...
544544
/// Some compilers will accept this code although a copy is returned. Use the RefAnyCast in
545545
/// these cases.
546546
{
547547
typedef typename TypeWrapper<ValueType>::TYPE NonRef;
548548

549-
return AnyCast<NonRef&>(const_cast<Any&>(operand));
549+
NonRef* result = AnyCast<NonRef>(&operand);
550+
if (!result) throw BadCastException("Failed to convert between Any types");
551+
return *result;
550552
}
551553

552554

553555
template <typename ValueType>
554-
ValueType AnyCast(Any& operand)
555-
/// AnyCast operator used to extract a copy of the ValueType from an Any&.
556+
ValueType AnyCast(const Any& operand)
557+
/// AnyCast operator used to extract a copy of the ValueType from an const Any&.
556558
///
557559
/// Example Usage:
558560
/// MyType tmp = AnyCast<MyType>(anAny).
559561
/// Will throw a BadCastException if the cast fails.
560-
/// Dont use an AnyCast in combination with references, i.e. MyType& tmp = ... or const MyType& tmp = ...
562+
/// Dont use an AnyCast in combination with references, i.e. MyType& tmp = ... or const MyType& = ...
561563
/// Some compilers will accept this code although a copy is returned. Use the RefAnyCast in
562564
/// these cases.
563565
{
564566
typedef typename TypeWrapper<ValueType>::TYPE NonRef;
565567

566-
NonRef* result = AnyCast<NonRef>(&operand);
567-
if (!result) throw BadCastException("Failed to convert between Any types");
568-
return *result;
568+
return AnyCast<NonRef&>(const_cast<Any&>(operand));
569569
}
570570

571571

JSON/include/Poco/JSON/Array.h

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ class JSON_API Array
186186
static Poco::Dynamic::Array makeArray(const JSON::Array::Ptr& arr);
187187
/// Utility function for creation of array.
188188

189+
void clear();
190+
/// Clears the contents of the array.
191+
189192
private:
190193
typedef SharedPtr<Poco::Dynamic::Array> ArrayPtr;
191194

@@ -389,6 +392,145 @@ class VarHolderImpl<JSON::Array::Ptr>: public VarHolder
389392
};
390393

391394

395+
template <>
396+
class VarHolderImpl<JSON::Array>: public VarHolder
397+
{
398+
public:
399+
VarHolderImpl(const JSON::Array& val): _val(val)
400+
{
401+
}
402+
403+
~VarHolderImpl()
404+
{
405+
}
406+
407+
const std::type_info& type() const
408+
{
409+
return typeid(JSON::Array);
410+
}
411+
412+
void convert(Int8&) const
413+
{
414+
throw BadCastException();
415+
}
416+
417+
void convert(Int16&) const
418+
{
419+
throw BadCastException();
420+
}
421+
422+
void convert(Int32&) const
423+
{
424+
throw BadCastException();
425+
}
426+
427+
void convert(Int64&) const
428+
{
429+
throw BadCastException();
430+
}
431+
432+
void convert(UInt8&) const
433+
{
434+
throw BadCastException();
435+
}
436+
437+
void convert(UInt16&) const
438+
{
439+
throw BadCastException();
440+
}
441+
442+
void convert(UInt32&) const
443+
{
444+
throw BadCastException();
445+
}
446+
447+
void convert(UInt64&) const
448+
{
449+
throw BadCastException();
450+
}
451+
452+
void convert(bool& value) const
453+
{
454+
value = _val.size() > 0;
455+
}
456+
457+
void convert(float&) const
458+
{
459+
throw BadCastException();
460+
}
461+
462+
void convert(double&) const
463+
{
464+
throw BadCastException();
465+
}
466+
467+
void convert(char&) const
468+
{
469+
throw BadCastException();
470+
}
471+
472+
void convert(std::string& s) const
473+
{
474+
std::ostringstream oss;
475+
_val.stringify(oss, 2);
476+
s = oss.str();
477+
}
478+
479+
void convert(DateTime& /*val*/) const
480+
{
481+
throw BadCastException("Cannot convert Array to DateTime");
482+
}
483+
484+
void convert(LocalDateTime& /*ldt*/) const
485+
{
486+
throw BadCastException("Cannot convert Array to LocalDateTime");
487+
}
488+
489+
void convert(Timestamp& /*ts*/) const
490+
{
491+
throw BadCastException("Cannot convert Array to Timestamp");
492+
}
493+
494+
VarHolder* clone(Placeholder<VarHolder>* pVarHolder = 0) const
495+
{
496+
return cloneHolder(pVarHolder, _val);
497+
}
498+
499+
const JSON::Array& value() const
500+
{
501+
return _val;
502+
}
503+
504+
bool isArray() const
505+
{
506+
return false;
507+
}
508+
509+
bool isInteger() const
510+
{
511+
return false;
512+
}
513+
514+
bool isSigned() const
515+
{
516+
return false;
517+
}
518+
519+
bool isNumeric() const
520+
{
521+
return false;
522+
}
523+
524+
bool isString() const
525+
{
526+
return false;
527+
}
528+
529+
private:
530+
JSON::Array _val;
531+
};
532+
533+
392534
}} // namespace Poco::JSON
393535

394536

JSON/include/Poco/JSON/Object.h

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ class JSON_API Object
206206
operator const Poco::DynamicStruct& () const;
207207
/// Cast operator to Poco::DynamiStruct.
208208

209+
void clear();
210+
/// Clears the contents of the object. Insertion order
211+
/// preservation property is left intact.
212+
209213
private:
210214
template <typename C>
211215
void doStringify(const C& container, std::ostream& out, unsigned int indent, int step) const
@@ -472,6 +476,148 @@ class VarHolderImpl<JSON::Object::Ptr>: public VarHolder
472476
};
473477

474478

479+
template <>
480+
class VarHolderImpl<JSON::Object>: public VarHolder
481+
{
482+
public:
483+
VarHolderImpl(const JSON::Object& val): _val(val)
484+
{
485+
}
486+
487+
~VarHolderImpl()
488+
{
489+
}
490+
491+
const std::type_info& type() const
492+
{
493+
return typeid(JSON::Object);
494+
}
495+
496+
void convert(Int8&) const
497+
{
498+
throw BadCastException();
499+
}
500+
501+
void convert(Int16&) const
502+
{
503+
throw BadCastException();
504+
}
505+
506+
void convert(Int32&) const
507+
{
508+
throw BadCastException();
509+
}
510+
511+
void convert(Int64&) const
512+
{
513+
throw BadCastException();
514+
}
515+
516+
void convert(UInt8&) const
517+
{
518+
throw BadCastException();
519+
}
520+
521+
void convert(UInt16&) const
522+
{
523+
throw BadCastException();
524+
}
525+
526+
void convert(UInt32&) const
527+
{
528+
throw BadCastException();
529+
}
530+
531+
void convert(UInt64&) const
532+
{
533+
throw BadCastException();
534+
}
535+
536+
void convert(bool& value) const
537+
{
538+
value = _val.size() > 0;
539+
}
540+
541+
void convert(float&) const
542+
{
543+
throw BadCastException();
544+
}
545+
546+
void convert(double&) const
547+
{
548+
throw BadCastException();
549+
}
550+
551+
void convert(char&) const
552+
{
553+
throw BadCastException();
554+
}
555+
556+
void convert(std::string& s) const
557+
{
558+
std::ostringstream oss;
559+
_val.stringify(oss, 2);
560+
s = oss.str();
561+
}
562+
563+
void convert(DateTime& /*val*/) const
564+
{
565+
//TODO: val = _val;
566+
throw NotImplementedException("Conversion not implemented: JSON:Object => DateTime");
567+
}
568+
569+
void convert(LocalDateTime& /*ldt*/) const
570+
{
571+
//TODO: ldt = _val.timestamp();
572+
throw NotImplementedException("Conversion not implemented: JSON:Object => LocalDateTime");
573+
}
574+
575+
void convert(Timestamp& /*ts*/) const
576+
{
577+
//TODO: ts = _val.timestamp();
578+
throw NotImplementedException("Conversion not implemented: JSON:Object => Timestamp");
579+
}
580+
581+
VarHolder* clone(Placeholder<VarHolder>* pVarHolder = 0) const
582+
{
583+
return cloneHolder(pVarHolder, _val);
584+
}
585+
586+
const JSON::Object& value() const
587+
{
588+
return _val;
589+
}
590+
591+
bool isArray() const
592+
{
593+
return false;
594+
}
595+
596+
bool isInteger() const
597+
{
598+
return false;
599+
}
600+
601+
bool isSigned() const
602+
{
603+
return false;
604+
}
605+
606+
bool isNumeric() const
607+
{
608+
return false;
609+
}
610+
611+
bool isString() const
612+
{
613+
return false;
614+
}
615+
616+
private:
617+
JSON::Object _val;
618+
};
619+
620+
475621
}} // namespace Poco::JSON
476622

477623

0 commit comments

Comments
 (0)