Exemplo n.º 1
0
std::string NetworkMessage::serialize()
{
    std::string serializedMessage;

    ptree pt;

    std::string strMsg="";

    switch(argumentType)
    {
    case ArgumentTypes::BookType:
        strMsg=getBook().serialize();
        break;
    case ArgumentTypes::BorrowType:
        strMsg=getBorrow().serialize();
        break;
    case ArgumentTypes::MemberType:
        strMsg=getMember().serialize();
    }

    pt.put ("networkMessage", strMsg);
    pt.put ("command",getCommadStr());

    std::ostringstream buf;
    write_json (buf, pt, false);
    serializedMessage = buf.str();

    return serializedMessage;
}
Exemplo n.º 2
0
static void write_json_object(FILE *file, ParsingObject *pego, char *source, int level) {
    if (pego[0]) {
        if (pego[0]->child_size > 0) {
            fprintf(file, "{");
            for (int i = 0; i < pego[0]->child_size; i++) {
                fprintf(file, "\n");
                write_json_indent(file, level + 1);
                fprintf(file, "\"%s\": ", pego[0]->child[i]->tag);
                write_json(file, &pego[0]->child[i], source, level + 2);
                if (i + 1 < pego[0]->child_size) {
                    fprintf(file, ",");
                }
            }
            fprintf(file, "\n");
            write_json_indent(file, level + 1);
            fprintf(file, "}");
        } else {
            if (pego[0]->value == NULL) {
                for (long j = pego[0]->start_pos; j < pego[0]->end_pos; j++) {
                    fprintf(file, "%c", source[j]);
                }
            } else {
                fprintf(file, "%s", pego[0]->value);
            }
        }
    }
}
Exemplo n.º 3
0
void
view_write_json ( view *v,
                  gboolean sendFull,
                  const gdouble user_offset,
                  FILE *f )
{
  gdouble offset;

#ifdef DEBUG_VIEW_JSON
  fprintf ( stderr, "user offset=%f\n", user_offset );
#endif

  g_rw_lock_reader_lock ( &(v->lock) );

  if ( user_offset == -1 )
    offset = v->most_recent_entry - v->length;
  else if ( user_offset > v->most_recent_entry )
    offset = v->most_recent_entry - 60;
  else
    offset = user_offset;

#ifdef DEBUG_VIEW_JSON
  fprintf ( stderr, "final offset=%f\n", offset );
#endif

  write_json ( v, sendFull, offset, f );

  g_rw_lock_reader_unlock ( &(v->lock) );
}
Exemplo n.º 4
0
size_t
bser_encoding_size(json_t* node)
{
    struct null_stream stream;
    stream.stream.write = null_stream_write;

    return write_json(node, &stream.stream);
}
Exemplo n.º 5
0
void test_rest(void)
{
    FILE *j;
    brtr_t b;
    off_t r;
    int err;

    unlink("qq2.qq");
    brtr_open(&b, "qq2.qq");

    r = brtr_get_root(&b);

    j = write_json("{\"a\":\"1\"}");
    r = borodb_put(&b, r, 0, "test", j, &err);
    do_test("borodb_put 1", !err);

    j = write_json("{\"b\":\"2\"}");
    r = borodb_put(&b, r, 0, "test2", j, &err);
    do_test("borodb_put 2", !err);

    j = write_json("{\"c\":\"3\"}");
    r = borodb_put(&b, r, 0, "test/subkey", j, &err);
    do_test("borodb_put 3", !err);

    j = write_json("{\"d\":\"4\"}");
    r = borodb_put(&b, r, 0, "test2/subkey", j, &err);
    do_test("borodb_put 4", !err);

/*
    {
        off_t r1, s1;
        int f;

        r1 = brtr_get_s(&b, r, "test2");

        brtr_node_stree(&b, r1, &s1, &f);

        s1 = brtr_set_s(&b, s1, "subkey", "1234", 0);

        r = brtr_set(&b, r, "test2", 5, &s1, sizeof(s1), f | BRTR_SET_KSTREE);
    }
*/
    brtr_set_root(&b, r);

    brtr_close(&b);
}
Exemplo n.º 6
0
bool write(std::ostream& to, generate_array& ga, bool indent)
{
    try {
        write_json(to, ga.get_root(), indent);
        return true;
    } catch (const std::exception&) {
        return false;
    }
}
Exemplo n.º 7
0
std::string Tag::toJsonString()
{
	std::stringstream ss;
	ptree pt;
	pt.put("Id", m_Id);
	pt.put("Name", m_Name);
	write_json(ss, pt, false);
	return ss.str();
}
Exemplo n.º 8
0
TEST_F( ZDevicesTest, json_diff_with_2_devices) {
	std::stringstream jsonOut {};

	zDevices->put(annunce1);
	zDevices->put(annunce2);
	ptree properties = zDevices->getDifferences(0);
	write_json(jsonOut, properties,false);
	ASSERT_THAT(jsonOut.str(), Eq("{\"token\":\"2\",\"zDevice\":{\"short_address\":\"2\",\"capability\":\"11\",\"extend_address\":\"03-04-05-06-07-08-09-0a\"},\"zDevice\":{\"short_address\":\"102\",\"capability\":\"111\",\"extend_address\":\"0b-0c-0d-0e-0f-10-11-12\"}}\n"));
}
Exemplo n.º 9
0
bool wwrite(std::wostream& to, wentry_writer& ga, bool indent)
{
    try {
        write_json(to, ga.node(), indent);
        return true;
    } catch (const std::exception&) {        
        return false;
    }
}
Exemplo n.º 10
0
pfs::error_code ubjson_ostream<OStreamType, JsonType>::write_array (json_type const & j)
{
    _os << UBJSON_CHAR_ARRAY_BEGIN;

    bool use_count_optimization = ((_flags & count_optimized) && !j.empty());
    bool is_special_case = false;

    // If a type is specified, a count must also be specified.
    // A type cannot be specified by itself.
    bool use_type_optimization  = ((_flags & type_optimized) && use_count_optimization);

    // Additional checks for type optimization
    if (use_type_optimization) {

        // This call is safe. Array is not empty as checked before
        int8_t first_prefix = prefix(*j.cbegin());
        use_type_optimization = pfs::all_of(j.cbegin()
                , j.cend()
                , compare_prefix(first_prefix));

        if (use_type_optimization) {
            _os << UBJSON_CHAR_TYPE;
            _os << first_prefix;

            // Check special cases
            if (first_prefix == UBJSON_CHAR_NOOP
                    || first_prefix == UBJSON_CHAR_NULL
                    || first_prefix == UBJSON_CHAR_TRUE
                    || first_prefix == UBJSON_CHAR_FALSE) {
                is_special_case = true;
            }
        }
    }

    if (use_count_optimization) {
        _os << UBJSON_CHAR_SIZE;
        write_integer(static_cast<typename json_type::integer_type>(j.size()), true);
    }

    // Ignore special cases: strongly-typed arrays of null, no-op and boolean values
    if (! is_special_case) {
        typename json_type::const_iterator first = j.cbegin();
        typename json_type::const_iterator last  = j.cend();

        for (; first != last; ++first)
            write_json(*first, !use_type_optimization);
    }

    // If a count is specified the container must not specify an end-marker.
    if (!use_count_optimization)
        _os << UBJSON_CHAR_ARRAY_END;

    return pfs::error_code();
}
Exemplo n.º 11
0
Status Config::getMD5(std::string& hash_string) {
  // Request an accessor to our own config, outside of an update.
  ConfigDataInstance config;

  std::stringstream out;
  write_json(out, config.data());

  hash_string = osquery::hashFromBuffer(
      HASH_TYPE_MD5, (void*)out.str().c_str(), out.str().length());

  return Status(0, "OK");
}
Exemplo n.º 12
0
Data CryConfig::save() const {
  ptree pt;

  pt.put("cryfs.rootblob", _rootBlob);
  pt.put("cryfs.key", _encKey);
  pt.put("cryfs.cipher", _cipher);
  pt.put("cryfs.version", _version);

  stringstream stream;
  write_json(stream, pt);
  return Data::LoadFromStream(stream);
}
Exemplo n.º 13
0
static size_t
write_pdu(json_t* root, size_t content_size, stream_t* stream)
{
    size_t hdr_bytes;
    size_t content_bytes;

    if ((hdr_bytes = write_header(content_size, stream)) > 0 &&
        (content_bytes = write_json(root, stream)) == content_size) {
        return hdr_bytes + content_bytes;
    } else {
        return 0;
    }
}
Exemplo n.º 14
0
std::string Order::toJsonString()
{
	std::stringstream ss;
	ptree pt;
	pt.put("Id", m_Id);
	pt.put("PetId", m_PetId);
	pt.put("Quantity", m_Quantity);
	pt.put("ShipDate", m_ShipDate);
	pt.put("Status", m_Status);
	pt.put("Complete", m_Complete);
	write_json(ss, pt, false);
	return ss.str();
}
Exemplo n.º 15
0
Data CryConfig::save() const {
  ptree pt;

  pt.put<string>("cryfs.rootblob", _rootBlob);
  pt.put<string>("cryfs.key", _encKey);
  pt.put<string>("cryfs.cipher", _cipher);
  pt.put<string>("cryfs.version", _version);
  pt.put<string>("cryfs.createdWithVersion", _createdWithVersion);
  pt.put<uint64_t>("cryfs.blocksizeBytes", _blocksizeBytes);
  pt.put<string>("cryfs.filesystemId", _filesystemId.ToString());

  stringstream stream;
  write_json(stream, pt);
  return Data::LoadFromStream(stream);
}
Exemplo n.º 16
0
std::string User::toJsonString()
{
	std::stringstream ss;
	ptree pt;
	pt.put("Id", m_Id);
	pt.put("Username", m_Username);
	pt.put("FirstName", m_FirstName);
	pt.put("LastName", m_LastName);
	pt.put("Email", m_Email);
	pt.put("Password", m_Password);
	pt.put("Phone", m_Phone);
	pt.put("UserStatus", m_UserStatus);
	write_json(ss, pt, false);
	return ss.str();
}
Exemplo n.º 17
0
static size_t
write_header(size_t content_size, stream_t* stream)
{
    const uint8_t magic[] = { 0x00, 0x01 };
    size_t node_bytes;

    json_t* content_size_node = json_integer(content_size);

    if (stream->write(stream, magic, SIZE_MAGIC) == SIZE_MAGIC &&
        (node_bytes = write_json(content_size_node, stream)) > 0) {
        return SIZE_MAGIC + node_bytes;
    } else {
        return 0;
    }
}
Exemplo n.º 18
0
std::string Borrow::serialize()
{
    std::string serializedBorrow;

    ptree pt;
    pt.put ("borrowDate",borrowDate);
    pt.put ("borrowedBook", Borrow::borrowedBook.serializeJson());
    pt.put ("borrowingMember", Borrow::borrowingMember.serialize());

    std::ostringstream buf;
    write_json (buf, pt, false);
    serializedBorrow = buf.str();

    return serializedBorrow;
}
Exemplo n.º 19
0
Status HTTPConfigPlugin::genConfig(std::map<std::string, std::string>& config) {
  boost::property_tree::ptree recv;
  for (int i = 0; i <= MAX_TRIES; i++) {
    if (i == MAX_TRIES) {
      return Status(1, "Could not get config");
    }
    if (runEnrollment(i == 0).ok() && getConfig(recv).ok()) {
      break;
    }
  }

  std::stringstream ss;
  write_json(ss, recv);
  config[FLAGS_enrollment_app_id] = ss.str();
  return Status(0, "OK");
}
Exemplo n.º 20
0
static void createDefaultConfig(std::string root)
{
    std::cout << "Creating default config" << std::endl;
    boost::filesystem::create_directory(root);

    boost::property_tree::ptree pt;
    boost::property_tree::ptree lib, libs;

    lib.put("", "libsqlite.so");
    libs.push_back(std::make_pair("", lib));

    pt.add_child("libs", libs);

    std::string dir = root + "/";
    std::string config = dir + "/nmbls.json";

    write_json(config, pt);
}
Exemplo n.º 21
0
 void process_file(PdfDocP doc, std::ostringstream& json) {
   ptree doc_node;
   doc_node.put("name", "document name");
   int num_pages = doc->GetNumPages();
   doc_node.put("num_pages", num_pages);
   for (auto i = 0; i < num_pages; i++) {
     ptree page_tree;
     PdfPageP page = doc->AcquirePage(i);
     if (!page)
       continue;
     process_page(page, page_tree);
     doc->ReleasePage(page);
     doc_node.add_child("page", page_tree);
   }
   ptree json_node;
   json_node.add_child("document", doc_node);
   write_json(json, json_node);
 }
Exemplo n.º 22
0
/**
 * Save the key data in a JSON like format. The filename is specified in
 * constructor YubikoOtpKeyConfig::YubikoOtpKeyConfig(const string& )
 */
void YubikoOtpKeyConfig::save() {
	BOOST_LOG_NAMED_SCOPE("YubikoOtpKeyConfig::save");
	const string myOutFile = checkFileName(true);
	ptree myTree;
	myTree.put(K_NM_DOC_PRIV_ID /*--->*/, getPrivateId());
	myTree.put(K_NM_DOC_PUB_ID /*---->*/, getPublicId());
	myTree.put(K_NM_DOC_SEC_KEY /*--->*/, getSecretKey());
	myTree.put(K_NM_DOC_TIMESTAMP /*->*/, getTimestamp().tstp_int);
	myTree.put(K_NM_DOC_SES_CNTR /*-->*/, getCounter());
	myTree.put(K_NM_DOC_CRC /*------->*/, getCrc());
	myTree.put(K_NM_DOC_RANDOM /*---->*/, getRandom());
	myTree.put(K_NM_DOC_USE_CNTR /*-->*/, getUseCounter());
	myTree.put(K_NM_DOC_DESC /*------>*/, getDescription());
	myTree.put(K_NM_DOC_SYS_USER /*-->*/, getSysUser());
	myTree.put(K_NM_DOC_VERS /*------>*/, K_VL_VERS);
	write_json(myOutFile, myTree);
	itsChangedFlag = false;
}
Exemplo n.º 23
0
    // store current parameters to disk
    void write_to_disk()
    {
        BOOST_ASSERT(!m_file_name.empty());

        if(m_dirty){
            // save current parameters to disk
            boost::property_tree::ptree pt;
            pt.put("header.device", m_device_name);
            pt.put("header.version", version_string());
            typedef std::map<std::pair<std::string, std::string>, uint_> map_type;
            for(map_type::const_iterator iter = m_cache.begin(); iter != m_cache.end(); ++iter){
                const std::pair<std::string, std::string> &key = iter->first;
                pt.add(key.first + "." + key.second, iter->second);
            }
            write_json(m_file_name, pt);

            m_dirty = false;
        }
    }
Exemplo n.º 24
0
static size_t
write_object(json_t* json, stream_t* stream)
{
    uint8_t tag = BSER_TAG_OBJECT;
    size_t bytes = 0;

    assert(json_is_object(json));

    if (stream->write(stream, &tag, SIZE_U8) == SIZE_U8) {
        size_t total_bytes = SIZE_U8;

        json_t* length_node = json_integer(json_object_size(json));
        size_t integer_length = write_integer(length_node, stream);
        total_bytes += integer_length;
        json_decref(length_node);

        if (integer_length > 0) {
            size_t val_bytes = 1;
            void *iter = json_object_iter(json);

            while (iter != NULL && val_bytes > 0) {
                val_bytes = 0;

                json_t* key_node = json_string(json_object_iter_key(iter));
                size_t key_bytes = write_string(key_node, stream);
                total_bytes += key_bytes;
                json_decref(key_node);

                if (key_bytes > 0) {
                    json_t* value = json_object_iter_value(iter);
                    val_bytes = write_json(value, stream);
                    total_bytes += val_bytes;
                }
                iter = json_object_iter_next(json, iter);
            }
            if (iter == NULL) {
                bytes = total_bytes;
            }
        }
    }
    return bytes;
}
Exemplo n.º 25
0
static void write_json_array(FILE *file, ParsingObject *pego, char *source, int level) {
    fprintf(file, "[");
    for (int i = 0; i < pego[0]->child_size; i++) {
        fprintf(file, "\n");
        write_json_indent(file, level + 1);
        fprintf(file, "{");
        fprintf(file, "\n");
        write_json_indent(file, level + 2);
        fprintf(file, "\"tag\": \"%s\", \"value\": ", pego[0]->child[i]->tag);
        write_json(file, &pego[0]->child[i], source, level + 3);
        fprintf(file, "\n");
        write_json_indent(file, level + 1);
        fprintf(file, "}");
        if (i + 1 < pego[0]->child_size) {
            fprintf(file, ",");
        }
    }
    fprintf(file, "\n");
    write_json_indent(file, level);
    fprintf(file, "]");
}
Exemplo n.º 26
0
    bool ConfigStorageManager::saveConfig(ConfigTree *saveTree, std::string configName)
    {
        CONFIGSYS_DEBUG_CALLS;

        std::string savePath = "ConfigurationFiles/" + configName + ".json";

        // save tree to file(s) as ptree<string>
        try
        {
            write_json(savePath, saveTree->getRoot());
        }
        catch(boost::property_tree::json_parser::json_parser_error &je)
        {
            std::cout   << "ConfigStorageManager::saveConfig(...): " 
                        << je.what() 
                        << std::endl;
            return false;
        }

        return true;
    }
Exemplo n.º 27
0
static struct io_plan *jcon_connected(struct io_conn *conn,
				      struct lightningd_state *dstate)
{
	struct json_connection *jcon;

	jcon = tal(dstate, struct json_connection);
	jcon->dstate = dstate;
	jcon->used = 0;
	jcon->buffer = tal_arr(jcon, char, 64);
	jcon->stop = false;
	jcon->current = NULL;
	jcon->log = new_log(jcon, dstate->log_record, "%sjcon fd %i:",
			    log_prefix(dstate->base_log), io_conn_fd(conn));
	list_head_init(&jcon->output);

	io_set_finish(conn, finish_jcon, jcon);

	return io_duplex(conn,
			 io_read_partial(conn, jcon->buffer,
					 tal_count(jcon->buffer),
					 &jcon->len_read, read_json, jcon),
			 write_json(conn, jcon));
}
Exemplo n.º 28
0
std::string Borrow::serialize(std::map <int,Borrow> borrowData)
{
    std::string serializedBook;
    std::string completeJson;

    typedef std::map<int,Borrow>::iterator it_type;

    for(it_type borrow = borrowData.begin(); borrow != borrowData.end(); borrow++) {
        Borrow borrowObj;
        borrowObj = borrow->second;
        std::string jsonStr = borrowObj.serialize();
        completeJson += jsonStr;
    }

    ptree pt;
    pt.put ("response",completeJson);

    std::ostringstream buf;
    write_json (buf, pt, false);
    serializedBook = buf.str();

    return serializedBook;
}
Exemplo n.º 29
0
	/* \fn  Get
	  \brief Get link para
	  \param[in] char* setting as json string
	  \return bool
	*/
	virtual bool Get(char* setting, long settingLen) 
	{
		try
		{
			string settingStr = setting;
			stringstream stream;
			stream << settingStr;
			property_tree::ptree pRoot;
			//read_json(stream, pRoot);
			if (m_section.try_lock())
			{
				pRoot.put("name", name);
				pRoot.put("frameTimes", frameTimes);
				pRoot.put("frameTime", frameTime);
				pRoot.put("includeTaskTime", includeTaskTime);
				pRoot.put("readAndUpdate", readAndUpdate);
				pRoot.put("readBlocked", readBlocked);
				pRoot.put("writeAndClear", writeAndClear);
				pRoot.put("writeBlocked", writeBlocked);
				write_json(stream, pRoot);
				//settingStr.clear();stream >> 
				settingStr= stream.str();
				strcpy_s(setting, settingLen, settingStr.data());
				m_section.unlock();
			}
			else
			{
				WRITE_LOG(severity_level::warning, "");
			}
		}
		catch (...)
		{
			WRITE_LOG(severity_level::error, "");
			return false;
		}
		return true;
	}
Exemplo n.º 30
0
static size_t
write_array(json_t* json, stream_t* stream)
{
    size_t bytes = 0;

    if (can_be_compact_array(json)) {
        bytes = write_compact_array(json, stream);
    } else {
        uint8_t tag = BSER_TAG_ARRAY;
        if (stream->write(stream, &tag, SIZE_U8) == SIZE_U8) {
            size_t int_bytes;

            size_t length = json_array_size(json);
            json_t* length_node = json_integer(length);

            int_bytes = write_integer(length_node, stream);
            json_decref(length_node);

            if (int_bytes > 0) {
                int i;
                size_t elem_bytes = 1;
                size_t elems_bytes = 0;

                for (i = 0; i < length && elem_bytes > 0; ++i) {
                    elem_bytes = write_json(json_array_get(json, i), stream);
                    elems_bytes += elem_bytes;
                }

                if (i == length) {
                    bytes = SIZE_U8 + int_bytes + elems_bytes;
                }
            }
        }
    }
    return bytes;
}