Advertisement
Guest User

Untitled

a guest
May 2nd, 2025
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.52 KB | None | 0 0
  1. #ifndef JSONPROCESSOR_H
  2. #define JSONPROCESSOR_H
  3.  
  4. #include "Objects/components.h"
  5. #include <string>
  6. #include "lib/mio.hpp"
  7. #include "Objects/jsonmodel.h"
  8.  
  9.  
  10. class JsonProcessor
  11. {
  12. public:
  13.     JsonProcessor(std::string path);
  14.     enum Status {
  15.         kInDictionary,
  16.         kInArray,
  17.         kInString
  18.     };
  19.     JsonModel* getModel();
  20.  
  21. private:
  22.     std::shared_ptr<Components> ParseJson(int startindex = 0);
  23.     mio::mmap_source mmap;
  24.     std::unique_ptr<JsonModel> m_model;
  25. };
  26.  
  27. #endif // JSONPROCESSOR_H
  28.  
  29.  
  30.  
  31.  
  32.  
  33. // jsonprocessor.cpp
  34.  
  35. #include "jsonprocessor.h"
  36.  
  37. #include <stack>
  38.  
  39.  
  40.  
  41. JsonProcessor::JsonProcessor(std::string path) {
  42.     std::error_code error;
  43.     mmap = mio::make_mmap_source(path, error);
  44.  
  45.     ParseJson();
  46. }
  47.  
  48. std::shared_ptr<Components> JsonProcessor::ParseJson(int startIndex) {
  49.     std::shared_ptr<Components> currentComponent = nullptr;
  50.  
  51.     std::stack<Status> state;
  52.     std::string key;
  53.     std::string currentString;
  54.     // Stores whether next item is value or not
  55.     bool valueIncoming = false;
  56.     for (int it = startIndex; it < mmap.size(); ++it) {
  57.         char character = mmap[it];
  58.  
  59.         // Capture characters for string
  60.         if (!state.empty() && state.top() == kInString && character != '"') {
  61.             currentString += character;
  62.             continue;
  63.         }
  64.  
  65.         switch (character) {
  66.         case '{':
  67.             // Generate child
  68.             if (state.empty()) {
  69.                 currentComponent = std::shared_ptr<Components>(new Components(Components::kDictionary));
  70.                 m_model = std::unique_ptr<JsonModel>(new JsonModel(currentComponent));
  71.             }
  72.             else {
  73.                 currentComponent = std::shared_ptr<Components>(new Components(Components::kDictionary, currentComponent, key));
  74.             }
  75.  
  76.             // Point parent to child
  77.             if (!state.empty()) {
  78.                 if (state.top() == kInDictionary) {
  79.                     currentComponent->parent()->addChild(currentComponent);
  80.                 }
  81.             }
  82.             state.push(kInDictionary);
  83.             valueIncoming = false;
  84.             break;
  85.  
  86.         case '}':
  87.             state.pop();
  88.             if (!state.empty()) {
  89.                 if (currentComponent == nullptr) { throw std::invalid_argument("Component pointer is null"); }
  90.                 // Set current to parent
  91.                 currentComponent = std::static_pointer_cast<Components>(std::shared_ptr<Components>(currentComponent->parent()));
  92.                 if (state.top() == kInDictionary) { valueIncoming = false; }
  93.             }
  94.             break;
  95.  
  96.         case '"':
  97.             // If is closing quote
  98.             if (!state.empty() && state.top() == kInString) {
  99.                 state.pop();
  100.                 valueIncoming = !valueIncoming;
  101.  
  102.                 // If key and value have been captured
  103.                 if (!valueIncoming) {
  104.                     // Create child to save key-value-pair
  105.                     std::shared_ptr<Components> child = std::shared_ptr<Components>(new Components(Components::kString, currentComponent, key));
  106.                     child->setValue(currentString);
  107.                     child->parent()->addChild(child);
  108.                 }
  109.                 key = currentString;
  110.                 currentString = "";
  111.             }
  112.             else {
  113.                 state.push(kInString);
  114.             }
  115.             break;
  116.         }
  117.     }
  118.     return currentComponent;
  119. }
  120.  
  121. JsonModel* JsonProcessor::getModel() {
  122.     return m_model.get();
  123. }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement