comparison OrthancServer/FromDcmtkBridge.cpp @ 1688:27d70e9ee2e4

DicomToJsonFormat enumeration
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 07 Oct 2015 15:06:31 +0200
parents 4d80fc990dae
children 26083d84d237
comparison
equal deleted inserted replaced
1687:4d80fc990dae 1688:27d70e9ee2e4
582 assert(parent.type() == Json::objectValue); 582 assert(parent.type() == Json::objectValue);
583 583
584 DicomTag tag(FromDcmtkBridge::GetTag(element)); 584 DicomTag tag(FromDcmtkBridge::GetTag(element));
585 const std::string formattedTag = tag.Format(); 585 const std::string formattedTag = tag.Format();
586 586
587 // This version of the code gives access to the name of the private tags 587 if (format == DicomToJsonFormat_Short)
588 {
589 parent[formattedTag] = Json::nullValue;
590 return parent[formattedTag];
591 }
592
593 // This code gives access to the name of the private tags
588 DcmTag tagbis(element.getTag()); 594 DcmTag tagbis(element.getTag());
589 const std::string tagName(tagbis.getTagName()); 595 const std::string tagName(tagbis.getTagName());
590 596
591 parent[formattedTag] = Json::objectValue; 597 switch (format)
592 Json::Value& node = parent[formattedTag]; 598 {
593 599 case DicomToJsonFormat_Simple:
594 if (element.isLeaf()) 600 parent[tagName] = Json::nullValue;
595 { 601 return parent[tagName];
596 node["Name"] = tagName; 602
597 603 case DicomToJsonFormat_Full:
598 if (tagbis.getPrivateCreator() != NULL) 604 {
599 { 605 parent[formattedTag] = Json::objectValue;
600 node["PrivateCreator"] = tagbis.getPrivateCreator(); 606 Json::Value& node = parent[formattedTag];
601 } 607
602 608 if (element.isLeaf())
603 return node; 609 {
604 } 610 node["Name"] = tagName;
605 else 611
606 { 612 if (tagbis.getPrivateCreator() != NULL)
607 node["Name"] = tagName; 613 {
608 node["Type"] = "Sequence"; 614 node["PrivateCreator"] = tagbis.getPrivateCreator();
609 node["Value"] = Json::arrayValue; 615 }
610 return node["Value"]; 616
617 return node;
618 }
619 else
620 {
621 node["Name"] = tagName;
622 node["Type"] = "Sequence";
623 node["Value"] = Json::nullValue;
624 return node["Value"];
625 }
626 }
627
628 default:
629 throw OrthancException(ErrorCode_ParameterOutOfRange);
611 } 630 }
612 } 631 }
613 632
614 633
615 static void LeafValueToJson(Json::Value& target, 634 static void LeafValueToJson(Json::Value& target,
616 const DicomValue& value, 635 const DicomValue& value,
617 DicomToJsonFormat format, 636 DicomToJsonFormat format,
618 unsigned int maxStringLength, 637 unsigned int maxStringLength)
619 Encoding encoding) 638 {
620 { 639 std::string content = value.AsString();
621 assert(target.type() == Json::objectValue); 640
622 641 switch (format)
623 if (value.IsNull()) 642 {
624 { 643 case DicomToJsonFormat_Short:
625 target["Type"] = "Null"; 644 case DicomToJsonFormat_Simple:
626 target["Value"] = Json::nullValue; 645 {
627 } 646 assert(target.type() == Json::nullValue);
628 else 647
629 { 648 if (!value.IsNull() &&
630 std::string s = value.AsString(); 649 (maxStringLength == 0 ||
631 if (maxStringLength == 0 || 650 content.size() <= maxStringLength))
632 s.size() <= maxStringLength) 651 {
633 { 652 target = content;
634 target["Type"] = "String"; 653 }
635 target["Value"] = s; 654
636 } 655 break;
637 else 656 }
638 { 657
639 target["Type"] = "TooLong"; 658 case DicomToJsonFormat_Full:
640 target["Value"] = Json::nullValue; 659 {
641 } 660 assert(target.type() == Json::objectValue);
661
662 if (value.IsNull())
663 {
664 target["Type"] = "Null";
665 target["Value"] = Json::nullValue;
666 }
667 else
668 {
669 if (maxStringLength == 0 ||
670 content.size() <= maxStringLength)
671 {
672 target["Type"] = "String";
673 target["Value"] = content;
674 }
675 else
676 {
677 target["Type"] = "TooLong";
678 target["Value"] = Json::nullValue;
679 }
680 }
681 break;
682 }
683
684 default:
685 throw OrthancException(ErrorCode_ParameterOutOfRange);
642 } 686 }
643 } 687 }
644 688
645 689
646 static void DatasetToJson(Json::Value& parent, 690 static void DatasetToJson(Json::Value& parent,
659 Json::Value& target = PrepareNode(parent, element, format); 703 Json::Value& target = PrepareNode(parent, element, format);
660 704
661 if (element.isLeaf()) 705 if (element.isLeaf())
662 { 706 {
663 std::auto_ptr<DicomValue> v(FromDcmtkBridge::ConvertLeafElement(element, encoding)); 707 std::auto_ptr<DicomValue> v(FromDcmtkBridge::ConvertLeafElement(element, encoding));
664 LeafValueToJson(target, *v, format, maxStringLength, encoding); 708 LeafValueToJson(target, *v, format, maxStringLength);
665 } 709 }
666 else 710 else
667 { 711 {
668 assert(target.type() == Json::arrayValue); 712 assert(target.type() == Json::nullValue);
713 target = Json::arrayValue;
669 714
670 // "All subclasses of DcmElement except for DcmSequenceOfItems 715 // "All subclasses of DcmElement except for DcmSequenceOfItems
671 // are leaf nodes, while DcmSequenceOfItems, DcmItem, DcmDataset 716 // are leaf nodes, while DcmSequenceOfItems, DcmItem, DcmDataset
672 // etc. are not." The following dynamic_cast is thus OK. 717 // etc. are not." The following dynamic_cast is thus OK.
673 DcmSequenceOfItems& sequence = dynamic_cast<DcmSequenceOfItems&>(element); 718 DcmSequenceOfItems& sequence = dynamic_cast<DcmSequenceOfItems&>(element);
686 DcmItem& item, 731 DcmItem& item,
687 DicomToJsonFormat format, 732 DicomToJsonFormat format,
688 unsigned int maxStringLength, 733 unsigned int maxStringLength,
689 Encoding encoding) 734 Encoding encoding)
690 { 735 {
691 parent = Json::objectValue; 736 assert(parent.type() == Json::objectValue);
692 737
693 for (unsigned long i = 0; i < item.card(); i++) 738 for (unsigned long i = 0; i < item.card(); i++)
694 { 739 {
695 DcmElement* element = item.getElement(i); 740 DcmElement* element = item.getElement(i);
696 ElementToJson(parent, *element, format, maxStringLength, encoding); 741 ElementToJson(parent, *element, format, maxStringLength, encoding);
701 void FromDcmtkBridge::ToJson(Json::Value& target, 746 void FromDcmtkBridge::ToJson(Json::Value& target,
702 DcmDataset& dataset, 747 DcmDataset& dataset,
703 DicomToJsonFormat format, 748 DicomToJsonFormat format,
704 unsigned int maxStringLength) 749 unsigned int maxStringLength)
705 { 750 {
751 target = Json::objectValue;
706 DatasetToJson(target, dataset, format, maxStringLength, DetectEncoding(dataset)); 752 DatasetToJson(target, dataset, format, maxStringLength, DetectEncoding(dataset));
707 } 753 }
708 754
709 755
710 756