comparison OrthancServer/FromDcmtkBridge.cpp @ 1687:4d80fc990dae

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 07 Oct 2015 14:29:09 +0200
parents 14a32b2fa63e
children 27d70e9ee2e4
comparison
equal deleted inserted replaced
1686:14a32b2fa63e 1687:4d80fc990dae
573 return new DicomNullValue; 573 return new DicomNullValue;
574 } 574 }
575 } 575 }
576 576
577 577
578 static void DatasetToJson(Json::Value& target, 578 static Json::Value& PrepareNode(Json::Value& parent,
579 DcmElement& element,
580 DicomToJsonFormat format)
581 {
582 assert(parent.type() == Json::objectValue);
583
584 DicomTag tag(FromDcmtkBridge::GetTag(element));
585 const std::string formattedTag = tag.Format();
586
587 // This version of the code gives access to the name of the private tags
588 DcmTag tagbis(element.getTag());
589 const std::string tagName(tagbis.getTagName());
590
591 parent[formattedTag] = Json::objectValue;
592 Json::Value& node = parent[formattedTag];
593
594 if (element.isLeaf())
595 {
596 node["Name"] = tagName;
597
598 if (tagbis.getPrivateCreator() != NULL)
599 {
600 node["PrivateCreator"] = tagbis.getPrivateCreator();
601 }
602
603 return node;
604 }
605 else
606 {
607 node["Name"] = tagName;
608 node["Type"] = "Sequence";
609 node["Value"] = Json::arrayValue;
610 return node["Value"];
611 }
612 }
613
614
615 static void LeafValueToJson(Json::Value& target,
616 const DicomValue& value,
617 DicomToJsonFormat format,
618 unsigned int maxStringLength,
619 Encoding encoding)
620 {
621 assert(target.type() == Json::objectValue);
622
623 if (value.IsNull())
624 {
625 target["Type"] = "Null";
626 target["Value"] = Json::nullValue;
627 }
628 else
629 {
630 std::string s = value.AsString();
631 if (maxStringLength == 0 ||
632 s.size() <= maxStringLength)
633 {
634 target["Type"] = "String";
635 target["Value"] = s;
636 }
637 else
638 {
639 target["Type"] = "TooLong";
640 target["Value"] = Json::nullValue;
641 }
642 }
643 }
644
645
646 static void DatasetToJson(Json::Value& parent,
579 DcmItem& item, 647 DcmItem& item,
580 DicomToJsonFormat format, 648 DicomToJsonFormat format,
581 unsigned int maxStringLength, 649 unsigned int maxStringLength,
582 Encoding encoding); 650 Encoding encoding);
583 651
584 static void ElementToJson(Json::Value& target, 652
653 static void ElementToJson(Json::Value& parent,
585 DcmElement& element, 654 DcmElement& element,
586 DicomToJsonFormat format, 655 DicomToJsonFormat format,
587 unsigned int maxStringLength, 656 unsigned int maxStringLength,
588 Encoding encoding) 657 Encoding encoding)
589 { 658 {
590 assert(target.type() == Json::objectValue); 659 Json::Value& target = PrepareNode(parent, element, format);
591
592 DicomTag tag(FromDcmtkBridge::GetTag(element));
593 const std::string formattedTag = tag.Format();
594
595 // This version of the code gives access to the name of the private tags
596 DcmTag tagbis(element.getTag());
597 const std::string tagName(tagbis.getTagName());
598 660
599 if (element.isLeaf()) 661 if (element.isLeaf())
600 { 662 {
601 Json::Value value(Json::objectValue);
602 value["Name"] = tagName;
603
604 if (tagbis.getPrivateCreator() != NULL)
605 {
606 value["PrivateCreator"] = tagbis.getPrivateCreator();
607 }
608
609 std::auto_ptr<DicomValue> v(FromDcmtkBridge::ConvertLeafElement(element, encoding)); 663 std::auto_ptr<DicomValue> v(FromDcmtkBridge::ConvertLeafElement(element, encoding));
610 if (v->IsNull()) 664 LeafValueToJson(target, *v, format, maxStringLength, encoding);
611 {
612 value["Type"] = "Null";
613 value["Value"] = Json::nullValue;
614 }
615 else
616 {
617 std::string s = v->AsString();
618 if (maxStringLength == 0 ||
619 s.size() <= maxStringLength)
620 {
621 value["Type"] = "String";
622 value["Value"] = s;
623 }
624 else
625 {
626 value["Type"] = "TooLong";
627 value["Value"] = Json::nullValue;
628 }
629 }
630
631 target[formattedTag] = value;
632 } 665 }
633 else 666 else
634 { 667 {
635 Json::Value children(Json::arrayValue); 668 assert(target.type() == Json::arrayValue);
636 669
637 // "All subclasses of DcmElement except for DcmSequenceOfItems 670 // "All subclasses of DcmElement except for DcmSequenceOfItems
638 // are leaf nodes, while DcmSequenceOfItems, DcmItem, DcmDataset 671 // are leaf nodes, while DcmSequenceOfItems, DcmItem, DcmDataset
639 // etc. are not." The following cast is thus OK. 672 // etc. are not." The following dynamic_cast is thus OK.
640 DcmSequenceOfItems& sequence = dynamic_cast<DcmSequenceOfItems&>(element); 673 DcmSequenceOfItems& sequence = dynamic_cast<DcmSequenceOfItems&>(element);
641 674
642 for (unsigned long i = 0; i < sequence.card(); i++) 675 for (unsigned long i = 0; i < sequence.card(); i++)
643 { 676 {
644 DcmItem* child = sequence.getItem(i); 677 DcmItem* child = sequence.getItem(i);
645 Json::Value& v = children.append(Json::objectValue); 678 Json::Value& v = target.append(Json::objectValue);
646 DatasetToJson(v, *child, format, maxStringLength, encoding); 679 DatasetToJson(v, *child, format, maxStringLength, encoding);
647 } 680 }
648 681 }
649 target[formattedTag]["Name"] = tagName; 682 }
650 target[formattedTag]["Type"] = "Sequence"; 683
651 target[formattedTag]["Value"] = children; 684
652 } 685 static void DatasetToJson(Json::Value& parent,
653 }
654
655
656 static void DatasetToJson(Json::Value& target,
657 DcmItem& item, 686 DcmItem& item,
658 DicomToJsonFormat format, 687 DicomToJsonFormat format,
659 unsigned int maxStringLength, 688 unsigned int maxStringLength,
660 Encoding encoding) 689 Encoding encoding)
661 { 690 {
662 target = Json::objectValue; 691 parent = Json::objectValue;
663 692
664 for (unsigned long i = 0; i < item.card(); i++) 693 for (unsigned long i = 0; i < item.card(); i++)
665 { 694 {
666 DcmElement* element = item.getElement(i); 695 DcmElement* element = item.getElement(i);
667 ElementToJson(target, *element, format, maxStringLength, encoding); 696 ElementToJson(parent, *element, format, maxStringLength, encoding);
668 } 697 }
669 } 698 }
670 699
671 700
672 void FromDcmtkBridge::ToJson(Json::Value& root, 701 void FromDcmtkBridge::ToJson(Json::Value& target,
673 DcmDataset& dataset, 702 DcmDataset& dataset,
674 DicomToJsonFormat format, 703 DicomToJsonFormat format,
675 unsigned int maxStringLength) 704 unsigned int maxStringLength)
676 { 705 {
677 DatasetToJson(root, dataset, format, maxStringLength, DetectEncoding(dataset)); 706 DatasetToJson(target, dataset, format, maxStringLength, DetectEncoding(dataset));
678 } 707 }
679 708
680 709
681 710
682 void FromDcmtkBridge::ToJson(Json::Value& target, 711 void FromDcmtkBridge::ToJson(Json::Value& target,