comparison PalantirServer/PalantirRestApi.cpp @ 35:f6d12037f886

full json vs. simplified json
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 30 Aug 2012 12:24:31 +0200
parents 96e57b863dd9
children c1097a676eca
comparison
equal deleted inserted replaced
34:96e57b863dd9 35:f6d12037f886
36 Json::StyledWriter writer; 36 Json::StyledWriter writer;
37 std::string s = writer.write(value); 37 std::string s = writer.write(value);
38 output.AnswerBufferWithContentType(s, "application/json"); 38 output.AnswerBufferWithContentType(s, "application/json");
39 } 39 }
40 40
41
42 static void SimplifyTagsRecursion(Json::Value& target,
43 const Json::Value& source)
44 {
45 assert(source.isObject());
46
47 target = Json::objectValue;
48 Json::Value::Members members = source.getMemberNames();
49
50 for (size_t i = 0; i < members.size(); i++)
51 {
52 const Json::Value& v = source[members[i]];
53 const std::string& name = v["Name"].asString();
54 const std::string& type = v["Type"].asString();
55
56 if (type == "String")
57 {
58 target[name] = v["Value"].asString();
59 }
60 else if (type == "TooLong" ||
61 type == "Null")
62 {
63 target[name] = Json::nullValue;
64 }
65 else if (type == "Sequence")
66 {
67 const Json::Value& array = v["Value"];
68 assert(array.isArray());
69
70 Json::Value children = Json::arrayValue;
71 for (size_t i = 0; i < array.size(); i++)
72 {
73 Json::Value c;
74 SimplifyTagsRecursion(c, array[i]);
75 children.append(c);
76 }
77
78 target[name] = children;
79 }
80 else
81 {
82 assert(0);
83 }
84 }
85 }
86
87
88 static void SimplifyTags(Json::Value& target,
89 const FileStorage& storage,
90 const std::string& fileUuid)
91 {
92 std::string s;
93 storage.ReadFile(s, fileUuid);
94
95 Json::Value source;
96 Json::Reader reader;
97 if (!reader.parse(s, source))
98 {
99 throw PalantirException("Corrupted JSON file");
100 }
101
102 SimplifyTagsRecursion(target, source);
103 }
104
105
41 bool PalantirRestApi::Store(Json::Value& result, 106 bool PalantirRestApi::Store(Json::Value& result,
42 const std::string& postData) 107 const std::string& postData)
43 { 108 {
44 // Prepare an input stream for the memory buffer 109 // Prepare an input stream for the memory buffer
45 DcmInputBufferStream is; 110 DcmInputBufferStream is;
466 531
467 else if (uri.size() == 3 && 532 else if (uri.size() == 3 &&
468 uri[0] == "instances" && 533 uri[0] == "instances" &&
469 (uri[2] == "file" || 534 (uri[2] == "file" ||
470 uri[2] == "tags" || 535 uri[2] == "tags" ||
471 uri[2] == "named-tags")) 536 uri[2] == "simplified-tags"))
472 { 537 {
473 std::string fileUuid, contentType; 538 std::string fileUuid, contentType;
474 if (uri[2] == "file") 539 if (uri[2] == "file")
475 { 540 {
476 existingResource = index_.GetDicomFile(fileUuid, uri[1]); 541 existingResource = index_.GetDicomFile(fileUuid, uri[1]);
477 contentType = "application/dicom"; 542 contentType = "application/dicom";
478 } 543 }
479 else if (uri[2] == "tags" || 544 else if (uri[2] == "tags" ||
480 uri[2] == "named-tags") 545 uri[2] == "simplified-tags")
481 { 546 {
482 existingResource = index_.GetJsonFile(fileUuid, uri[1]); 547 existingResource = index_.GetJsonFile(fileUuid, uri[1]);
483 contentType = "application/json"; 548 contentType = "application/json";
484 } 549 }
485 550
486 if (existingResource) 551 if (existingResource)
487 { 552 {
488 output.AnswerFile(storage_, fileUuid, contentType); 553 if (uri[2] == "simplified-tags")
489 return; 554 {
555 Json::Value v;
556 SimplifyTags(v, storage_, fileUuid);
557 SendJson(output, v);
558 return;
559 }
560 else
561 {
562 output.AnswerFile(storage_, fileUuid, contentType);
563 return;
564 }
490 } 565 }
491 } 566 }
492 567
493 568
494 else if (uri.size() == 3 && 569 else if (uri.size() == 3 &&