背景

C/C++客户端需要接收和发送JSON格式的数据到后端以实现通讯和数据交互。C++没有现成的处理JSON格式数据的接口,直接引用第三方库还是避免不了拆解拼接。考虑到此项目将会有大量JSON数据需要处理,避免不了重复性的拆分拼接。所以打算封装一套C++结构体对象转JSON数据、JSON数据直接装C++结构体对象的接口,类似于数据传输中常见的序列化和反序列化,以方便后续处理数据,提高开发效率。

设计

目标:

  1. 通过简单接口就能将C++结构体对象实例转换为JSON字符串数据,或将一串JSON字符串数据加载赋值到一个C++结构体对象实例。理想接口:Json2Object(inJsonString, outStructObject),或者Object2Json(inStructObject, outJsonString)
  2. 支持内置基本类型如bool,int,double的Json转换,支持自定义结构体的Json转换,支持上述类型作为元素数组的Json转换,以及支持嵌套的结构体的Json转换

效果:

先上单元测试代码

TEST_CASE("解析结构体数组到JSON串", "[json]")
{
struct DemoChildrenObject
{
bool boolValue;
int intValue;
std::string strValue;
/*JSON相互转换成员变量声明(必需)*/
JSONCONVERT2OBJECT_MEMEBER_REGISTER(boolValue, intValue, strValue)
}; struct DemoObjct
{
bool boolValue;
int intValue;
std::string strValue;
/*嵌套的支持JSON转换的结构体成员变量,数组形式*/
std::vector< DemoChildrenObject> children; /*JSON相互转换成员变量声明(必需)*/
JSONCONVERT2OBJECT_MEMEBER_REGISTER(boolValue, intValue, strValue, children)
}; DemoObjct demoObj;
/*开始对demoObj对象的成员变量进行赋值*/
demoObj.boolValue = true;
demoObj.intValue = 321;
demoObj.strValue = "hello worLd"; DemoChildrenObject child1;
child1.boolValue = true;
child1.intValue = 1000;
child1.strValue = "hello worLd child1"; DemoChildrenObject child2;
child2.boolValue = true;
child2.intValue = 30005;
child2.strValue = "hello worLd child2"; demoObj.children.push_back(child1);
demoObj.children.push_back(child2);
/*结束对demoObj对象的成员变量的赋值*/ std::string jsonStr;
/*关键转换函数*/
REQUIRE(Object2Json(jsonStr, demoObj));
std::cout << "returned json format: " << jsonStr << std::endl; /*打印的内容如下:
returned json format: {
"boolValue" : true,
"children" : [
{
"boolValue" : true,
"intValue" : 1000,
"strValue" : "hello worLd child1"
},
{
"boolValue" : true,
"intValue" : 30005,
"strValue" : "hello worLd child2"
}
],
"intValue" : 321,
"strValue" : "hello worLd"
}
*/ DemoObjct demoObj2;
/*关键转换函数*/
REQUIRE(Json2Object(demoObj2, jsonStr)); /*校验转换后的结构体变量中各成员变量的内容是否如预期*/
REQUIRE(demoObj2.boolValue == true);
REQUIRE(demoObj2.intValue == 321);
REQUIRE(demoObj2.strValue == "hello worLd"); REQUIRE(demoObj2.children.size() == 2); REQUIRE(demoObj.children[0].boolValue == true);
REQUIRE(demoObj.children[0].intValue == 1000);
REQUIRE(demoObj.children[0].strValue == "hello worLd child1"); REQUIRE(demoObj.children[1].boolValue == true);
REQUIRE(demoObj.children[1].intValue == 30005);
REQUIRE(demoObj.children[1].strValue == "hello worLd child2");
}

实现

本次我们只关注怎么友好地在结构体与Json字符串之间进行转换,而不深入关注JSon字符串具体如何与基本数据类型进行转换。这个已经有不少的第三方库帮我们解决这个问题,如cJSONJsoncpprapidjson,不必再重复造轮子。

此次我们选择了JsonCPP作为底层的JSON解析支持,如果想替换成其他三方库也比较简单,修改对应嵌入的内容即可。

我们的目标是实现两个接口:

  • Json2Object(inJsonString, outStructObject)

  • Object2Json(inStructObject, outJsonString)

结合JsonCPP自身定义的类型,我们进一步需要实现的是:

  • Json2Object(const Json::Value& jsonTypeValue, outStructObject)

  • Object2Json(inStructObject, const std::string& key, Json::Value& jsonTypeValue)

基本数据类型转换

对于如bool、int、double、string等基本数据类型,该实现均较为简单:

/*int 类型支持*/
static bool Json2Object(int& aimObj, const Json::Value& jsonTypeValue)
{
if (jsonTypeValue.isNull() || !jsonTypeValue.isInt()) {
return false;
} else {
aimObj = jsonTypeValue.asInt();
return true;
}
} static bool Object2Json(Json::Value& jsonTypeValue, const std::string& key, const int& value)
{
jsonTypeValue[key] = value;
return true;
} /*std::string 字符串类型支持*/
static bool Json2Object(std::string& aimObj, const Json::Value& jsonTypeValue)
{
if (jsonTypeValue.isNull() || !jsonTypeValue.isString()) {
return false;
} else {
aimObj = jsonTypeValue.asString();
return true;
}
} static bool Object2Json(Json::Value& jsonTypeValue, const std::string& key, const std::string& value)
{
jsonTypeValue[key] = value;
return true;
}

自定义数据结构类型

对于自定义的结构体类型,我们要做的就是要保证其成员变量能够与JSON节点一一对应,并能够匹配进行数据填充。

   /*Json字符串:
{
"boolValue" : true,
"intValue" : 1234,
"strValue" : "demo object!"
}*/ struct DemoObjct
{
bool boolValue;
int intValue;
std::string strValue;
};

如上面示例,在相互转换过程中,"boolValue"能与DemoObjct对象中名为boolValue的成员变量对应,"strValue"与DemoObjct对象中名为strValue的成员变量对应。

正常情况下,对于这种场景,我们只能对DemoObjct结构体额外实现处理函数进行数据转换,因不同的结构体声明定义的成员变量都不一样,所以针对每个结构体均需要单独实现,工作繁琐,不通用。

从这里下手,我们要做的就是“隐藏”针对类结构体实现的转换函数,利用语言自身的特性(函数模板等)让他们帮我们去做这些事情。

  1. 声明转换成员函数,在这个成员函数实现里,让每个成员变量能从JSON原生数据中读取或写入值。
  2. 注册成员变量,目的是让转换成员函数知道需要处理哪些成员变量,每个成员变量又对应JSON原生数据中的哪个节点字段,以便匹配读写。
  3. 在外部调用Json2ObjectObject2Json函数时,触发调用该转换成员函数,以便填充或输出成员变量的内容。

把大象装进冰箱里只需要三步,我们来看这三步怎么走。

成员变量处理

  • 考虑到每个结构体的成员变量类型和数量不可控,并且需要将每个成员变量作为左值(Json2Object时),不能简单采用数组枚举方式处理,可以采用C++11的特性——可变参数模板,从里到外遍历处理每个成员变量
template <typename T>
static bool JsonParse(const std::vector<std::string>& names, int index, const Json::Value& jsonTypeValue, T& arg)
{
const auto key = names[index];
if (!jsonTypeValue.isMember(key) || Json2Object(arg, jsonTypeValue[key])) {
return true;
} else {
return false;
}
} template <typename T, typename... Args>
static bool JsonParse(const std::vector<std::string>& names, int index, const Json::Value& jsonTypeValue, T& arg, Args&... args)
{
if (!JsonParse(names, index, jsonTypeValue, arg)) {
return false;
} else {
return JsonParse(names, index + 1, jsonTypeValue, args...);
}
}
  • 成员变量与JSON原生节点key字段有对应关系,初步先简单考虑,将成员变量名称先视为JSON中对应节点的key名称。这个可以通过宏定义的特性实现,将声明注册的成员变量内容作为字符串拆分出key名称列表。
#define JSONCONVERT2OBJECT_MEMEBER_REGISTER(...)  \
bool ParseHelpImpl(const Json::Value& jsonTypeValue)
{
std::vector<std::string> names = Member2KeyParseWithStr(#__VA_ARGS__);
return JsonParse(names, 0, jsonTypeValue, __VA_ARGS__);
}

成员变量注册

例如DemoObjct这个类结构体,添加JSONCONVERT2OBJECT_MEMEBER_REGISTER并带上成员变量的注册声明:

   struct DemoObjct
{
bool boolValue;
int intValue;
std::string strValue; JSONCONVERT2OBJECT_MEMEBER_REGISTER(boolValue, intValue, strValue) };

等同于:

   struct DemoObjct
{
bool boolValue;
int intValue;
std::string strValue; bool ParseHelpImpl(const Json::Value& jsonTypeValue,
std::vector<std::string> &names)
{
names = Member2KeyParseWithStr("boolValue, intValue, strValue");
//names 得到 ["boolValue","intValue", "strValue"]
//然后带着这些key逐一从Json中取值赋值到成员变量中
return JsonParse(names, 0, jsonTypeValue, boolValue, intValue, strValue);
}
};

模板匹配防止编译报错

到目前为止,核心的功能已经实现。如果目标结构体类未添加JSON转换的声明注册,外部在使用Json2Object接口时会导致编译报错,提示找不到ParseHelpImpl这个成员函数的声明定义……,我们可以采用enable_if来给未声明注册宏的结构体提供缺省函数。

template <typename TClass, typename enable_if<HasConverFunction<TClass>::has, int>::type = 0>
static inline bool Json2Object(TClass& aimObj, const Json::Value& jsonTypeValue)
{
std::vector<std::string> names = PreGetCustomMemberNameIfExists(aimObj);
return aimObj.JSONCONVERT2OBJECT_MEMEBER_REGISTER_RESERVERD_IMPLE(jsonTypeValue, names);
} template <typename TClass, typename enable_if<!HasConverFunction<TClass>::has, int>::type = 0>
static inline bool Json2Object(TClass& aimObj, const Json::Value& jsonTypeValue)
{
return false;
}

成员变量匹配Key重命名

目前的实现均为将成员变量的名称作为JSON串中的Key名称,为灵活处理,再补充一个宏用于重新声明结构体成员变量中对应到JSON串中的key,例如:

    struct DemoObjct
{
bool boolValue;
int intValue;
std::string strValue; JSONCONVERT2OBJECT_MEMEBER_REGISTER(boolValue, intValue, strValue)
/*重新声明成员变量对应到JSON串的key,注意顺序一致*/
JSONCONVERT2OBJECT_MEMEBER_RENAME_REGISTER("bValue", "iValue", "sValue")
}; DemoObjct demoObj;
/*boolValue <--> bValue; intValue <--> iValue; ...*/
REQUIRE(Json2Object(demoObj, std::string("{\"bValue\":true, \"iValue\":1234, \"sValue\":\"demo object!\"}")));
REQUIRE(demoObj.boolValue == true);
REQUIRE(demoObj.intValue == 1234);
REQUIRE(demoObj.strValue == "demo object!");

Object2Json实现

上面提到为大多为实现Json2Object接口所提供的操作,从结构体对象转成Json也是类似的操作,这里就不再阐述,详细可参考源码。

亮点

  • 简化C++对JSON数据的处理,屏蔽注意拆分处理JSON数据的操作;
  • 提供简易接口,从结构体到JSON串、JSON串转结构体切换自如

源码



#include "json/json.h"
#include <string>
#include <vector>
#include <initializer_list> #define JSONCONVERT2OBJECT_MEMEBER_REGISTER(...) \
bool JSONCONVERT2OBJECT_MEMEBER_REGISTER_RESERVERD_IMPLE(const Json::Value& jsonTypeValue, std::vector<std::string> &names) \
{ \
if(names.size() <= 0) { \
names = Member2KeyParseWithStr(#__VA_ARGS__); \
} \
return JsonParse(names, 0, jsonTypeValue, __VA_ARGS__); \
} \
bool OBJECTCONVERT2JSON_MEMEBER_REGISTER_RESERVERD_IMPLE(Json::Value& jsonTypeValue, std::vector<std::string> &names) const \
{ \
if(names.size() <= 0) { \
names = Member2KeyParseWithStr(#__VA_ARGS__); \
} \
return ParseJson(names, 0, jsonTypeValue, __VA_ARGS__); \
} \ #define JSONCONVERT2OBJECT_MEMEBER_RENAME_REGISTER(...) \
std::vector<std::string> JSONCONVERT2OBJECT_MEMEBER_RENAME_REGISTER_RESERVERD_IMPLE() const \
{ \
return Member2KeyParseWithMultiParam({ __VA_ARGS__ }); \
} namespace JSON
{
template <bool, class TYPE = void>
struct enable_if
{
}; template <class TYPE>
struct enable_if<true, TYPE>
{
typedef TYPE type;
};
} //JSON template <typename T>
struct HasConverFunction
{
template <typename TT>
static char func(decltype(&TT::JSONCONVERT2OBJECT_MEMEBER_REGISTER_RESERVERD_IMPLE)); //@1 template <typename TT>
static int func(...); //@2 const static bool has = (sizeof(func<T>(NULL)) == sizeof(char)); template <typename TT>
static char func2(decltype(&TT::JSONCONVERT2OBJECT_MEMEBER_RENAME_REGISTER_RESERVERD_IMPLE)); //@1
template <typename TT>
static int func2(...); //@2
const static bool has2 = (sizeof(func2<T>(NULL)) == sizeof(char));
}; static std::vector<std::string> Member2KeyParseWithMultiParam(std::initializer_list<std::string> il)
{
std::vector<std::string> result;
for (auto it = il.begin(); it != il.end(); it++) {
result.push_back(*it);
}
return result;
} inline static std::string NormalStringTrim(std::string const& str)
{
static char const* whitespaceChars = "\n\r\t ";
std::string::size_type start = str.find_first_not_of(whitespaceChars);
std::string::size_type end = str.find_last_not_of(whitespaceChars);
return start != std::string::npos ? str.substr(start, 1 + end - start) : std::string();
} inline static std::vector<std::string> NormalStringSplit(std::string str, char splitElem)
{
std::vector<std::string> strs;
std::string::size_type pos1, pos2;
pos2 = str.find(splitElem);
pos1 = 0;
while (std::string::npos != pos2) {
strs.push_back(str.substr(pos1, pos2 - pos1));
pos1 = pos2 + 1;
pos2 = str.find(splitElem, pos1);
}
strs.push_back(str.substr(pos1));
return strs;
} static std::vector<std::string> Member2KeyParseWithStr(const std::string& values)
{
std::vector<std::string> result;
auto enumValues = NormalStringSplit(values, ',');
result.reserve(enumValues.size());
for (auto const& enumValue : enumValues) {
result.push_back(NormalStringTrim(enumValue));
}
return result;
} ////////////////////////////////////////////////////////////////////////////// static bool Json2Object(bool& aimObj, const Json::Value& jsonTypeValue)
{
if (jsonTypeValue.isNull() || !jsonTypeValue.isBool()) {
return false;
} else {
aimObj = jsonTypeValue.asBool();
return true;
}
}
static bool Object2Json(Json::Value& jsonTypeValue, const std::string& key, bool value)
{
jsonTypeValue[key] = value;
return true;
} static bool Json2Object(int& aimObj, const Json::Value& jsonTypeValue)
{
if (jsonTypeValue.isNull() || !jsonTypeValue.isInt()) {
return false;
} else {
aimObj = jsonTypeValue.asInt();
return true;
}
}
static bool Object2Json(Json::Value& jsonTypeValue, const std::string& key, const int& value)
{
jsonTypeValue[key] = value;
return true;
} static bool Json2Object(unsigned int& aimObj, const Json::Value& jsonTypeValue)
{
if (jsonTypeValue.isNull() || !jsonTypeValue.isUInt()) {
return false;
} else {
aimObj = jsonTypeValue.asUInt();
return true;
}
}
static bool Object2Json(Json::Value& jsonTypeValue, const std::string& key, const unsigned int& value)
{
jsonTypeValue[key] = value;
return true;
} static bool Json2Object(double& aimObj, const Json::Value& jsonTypeValue)
{
if (jsonTypeValue.isNull() || !jsonTypeValue.isDouble()) {
return false;
} else {
aimObj = jsonTypeValue.asDouble();
return true;
}
}
static bool Object2Json(Json::Value& jsonTypeValue, const std::string& key, const double& value)
{
jsonTypeValue[key] = value;
return true;
} static bool Json2Object(std::string& aimObj, const Json::Value& jsonTypeValue)
{
if (jsonTypeValue.isNull() || !jsonTypeValue.isString()) {
return false;
} else {
aimObj = jsonTypeValue.asString();
return true;
}
}
static bool Object2Json(Json::Value& jsonTypeValue, const std::string& key, const std::string& value)
{
jsonTypeValue[key] = value;
return true;
} template <typename TClass, typename JSON::enable_if<HasConverFunction<TClass>::has2, int>::type = 0>
static inline std::vector<std::string> PreGetCustomMemberNameIfExists(const TClass& aimObj)
{
return aimObj.JSONCONVERT2OBJECT_MEMEBER_RENAME_REGISTER_RESERVERD_IMPLE();
} template <typename TClass, typename JSON::enable_if<!HasConverFunction<TClass>::has2, int>::type = 0>
static inline std::vector<std::string> PreGetCustomMemberNameIfExists(const TClass& aimObj)
{
return std::vector<std::string>();
} template <typename TClass, typename JSON::enable_if<HasConverFunction<TClass>::has, int>::type = 0>
static inline bool Json2Object(TClass& aimObj, const Json::Value& jsonTypeValue)
{
std::vector<std::string> names = PreGetCustomMemberNameIfExists(aimObj);
return aimObj.JSONCONVERT2OBJECT_MEMEBER_REGISTER_RESERVERD_IMPLE(jsonTypeValue, names);
} template <typename TClass, typename JSON::enable_if<!HasConverFunction<TClass>::has, int>::type = 0>
static inline bool Json2Object(TClass& aimObj, const Json::Value& jsonTypeValue)
{
return false;
} template <typename T>
static bool Json2Object(std::vector<T>& aimObj, const Json::Value& jsonTypeValue)
{
if (jsonTypeValue.isNull() || !jsonTypeValue.isArray()) {
return false;
} else {
aimObj.clear();
bool result(true);
for (int i = 0; i < jsonTypeValue.size(); ++i) {
T item;
if (!Json2Object(item, jsonTypeValue[i])) {
result = false;
}
aimObj.push_back(item);
}
return result;
}
} template <typename T>
static bool JsonParse(const std::vector<std::string>& names, int index, const Json::Value& jsonTypeValue, T& arg)
{
const auto key = names[index];
if (!jsonTypeValue.isMember(key) || Json2Object(arg, jsonTypeValue[key])) {
return true;
} else {
return false;
}
} template <typename T, typename... Args>
static bool JsonParse(const std::vector<std::string>& names, int index, const Json::Value& jsonTypeValue, T& arg, Args&... args)
{
if (!JsonParse(names, index, jsonTypeValue, arg)) {
return false;
} else {
return JsonParse(names, index + 1, jsonTypeValue, args...);
}
} /** Provider interface*/
template<typename TClass>
bool Json2Object(TClass& aimObj, const std::string& jsonTypeStr)
{
Json::Reader reader;
Json::Value root; if (!reader.parse(jsonTypeStr, root) || root.isNull()) {
return false;
}
return Json2Object(aimObj, root);
} static bool GetJsonRootObject(Json::Value& root, const std::string& jsonTypeStr)
{
Json::Reader reader;
if (!reader.parse(jsonTypeStr, root)) {
return false;
}
return true;
} //////////////////////////////////////////////////////////////////////// template <typename TClass, typename JSON::enable_if<HasConverFunction<TClass>::has, int>::type = 0>
static inline bool Object2Json(Json::Value& jsonTypeOutValue, const std::string& key, const TClass& objValue)
{
std::vector<std::string> names = PreGetCustomMemberNameIfExists(objValue);
if (key.empty()) {
return objValue.OBJECTCONVERT2JSON_MEMEBER_REGISTER_RESERVERD_IMPLE(jsonTypeOutValue, names);
} else {
Json::Value jsonTypeNewValue;
const bool result = objValue.OBJECTCONVERT2JSON_MEMEBER_REGISTER_RESERVERD_IMPLE(jsonTypeNewValue, names);
if (result) {
jsonTypeOutValue[key] = jsonTypeNewValue;
}
return result;
}
} template <typename TClass, typename JSON::enable_if<!HasConverFunction<TClass>::has, int>::type = 0>
static inline bool Object2Json(Json::Value& jsonTypeOutValue, const std::string& key, const TClass& objValue)
{
return false;
} template <typename T>
static bool Object2Json(Json::Value& jsonTypeOutValue, const std::string& key, const std::vector<T>& objValue)
{
bool result(true);
for (int i = 0; i < objValue.size(); ++i) {
Json::Value item;
if (!Object2Json(item, "", objValue[i])) {
result = false;
} else {
if (key.empty()) jsonTypeOutValue.append(item);
else jsonTypeOutValue[key].append(item);
}
}
return result;
} template <typename T>
static bool ParseJson(const std::vector<std::string>& names, int index, Json::Value& jsonTypeValue, const T& arg)
{
if (names.size() > index) {
const std::string key = names[index];
return Object2Json(jsonTypeValue, key, arg);
} else {
return false;
}
} template <typename T, typename... Args>
static bool ParseJson(const std::vector<std::string>& names, int index, Json::Value& jsonTypeValue, T& arg, Args&... args)
{
if (names.size() - (index + 0) != 1 + sizeof...(Args)) {
return false;
}
const std::string key = names[index];
Object2Json(jsonTypeValue, key, arg); return ParseJson(names, index + 1, jsonTypeValue, args...);
} /** Provider interface*/
template<typename T>
bool Object2Json(std::string& jsonTypeStr, const T& obj)
{
//std::function<Json::Value()>placehoder = [&]()->Json::Value { return Json::Value(); };
//auto func = [&](std::function<Json::Value()>f) { return f(); };
//Json::Value val = func(placehoder); Json::StyledWriter writer;
Json::Value root;
const bool result = Object2Json(root, "", obj);
if (result) {
jsonTypeStr = writer.write(root);
}
return result;
}

参考文档

C++实现对Json数据的友好处理的更多相关文章

  1. js实现对json数据的序列化(兼容ie6以上浏览器)

    /** * 增加对JSON数据的序列化方法, * 主要用于IE6.7不支持JSON对象的浏览器 */ var xue = xue || {};xue.json = xue.json || {}; xu ...

  2. C# - VS2019 通过DataGridView实现对Oracle数据表的增删改查

    前言 通过VS2019建立WinFrm应用程序,搭建桌面程序后,通过封装数据库操作OracleHelper类和业务逻辑操作OracleSQL类,进而通过DataGridView实现对Oracle数据表 ...

  3. iOS:二叉树多级表格的使用,使用三方库TreeTableView-master实现对json解析数据的递归遍历整理成树状结构

    在项目中,我们有时需要使用二叉树来实现多级表格的递归遍历查询,如果对二叉树比较懂,那么写起来其实也不费事,为了节省开发时间,下面介绍一下第三方库TreeTableView-master,这个三方库上给 ...

  4. python实现对excel数据进行修改/添加

    import osimport xlrdfrom xlutils.copy import copydef base_dir(filename=None): return os.path.join(os ...

  5. 【OPCAutomation】 使用OPCAutomation实现对OPC数据的访问

    折腾了一段时间研究OPC,理清了下位机.OPCServer 和OPCClient的关系和通信模型,终于能够来写一篇相关的博客了. 我们使用西门子的 S7 200 SMART作为下位机端,通过3G路由器 ...

  6. SQLite帮助类SQlitehelper 实现对SQLite数据的增删改查

    public class SQLiteHelper { public const string sConn = "Data Source=" + @"path" ...

  7. 如何处理json数据

    1. 前台处理方式之一: ★jQuery.parseJSON(json)                     var parsej = $.parseJSON(data);             ...

  8. SQL操作Json数据

    转载自: http://blog.csdn.net/yapingxin/article/details/16913275 有小改动.. 支持复杂结构的使用.. 使用Parent_ID来对应Object ...

  9. 【spark】文件读写和JSON数据解析

    1.读文件 通过 sc.textFile(“file://") 方法来读取文件到rdd中. val lines = sc.textFile("file://")//文件地 ...

随机推荐

  1. 开源社区合入patch的步骤

    以Ranger项目为例,说明开源社区合入patch的详细步骤. 1.reviews页面下载patch 进入到review页面:https://reviews.apache.org/r/67919/ 点 ...

  2. FastAPI快速查阅

    官方文档主要侧重点是循序渐进地学习FastAPI, 不利于有其他框架使用经验的人快速查阅 故本文与官方文档不一样, 并补充了一些官方文档没有的内容 安装 包括安装uvicorn $pip instal ...

  3. VMware_克隆机器后主机Ping不同虚拟机,虚拟机能Ping通主机

    使用vm的克隆功能克隆一个系统,因为我克隆的系统使用的是静态IP,所以修改克隆机的ip地址,并且也修改MAC地址,启动后两个虚拟机,ping了下主机IP能Ping通,但是使用主机Ping虚拟机时发现P ...

  4. 区别对待 .gz 文件 和 .tar.gz 文件

    #检测 tar tvf xxx.tar.gz #解压 tar zxvf #检测 gunzip -tv yyy.gz #解压 gunzip yyy.gz

  5. 实验 2 :Mininet 实验 —— 拓扑的命令脚本

    实验2: Mininet 实验--拓扑的命令脚本 一.实验目的 掌握 Mininet 的自定义拓扑生成方法:命令行创建.Python 脚本编写 二 .实验任务 通过使用命令行创建.Python 脚本编 ...

  6. vue 图片拖拽和滚轮缩放

    这里注意如果自己的页面有滚动条,一定阻止滚动事件的默认行为,否则缩放图片的时候,页面会跟着滚动@mousewheel.prevent 阻止默认行为 <div ref="imgWrap& ...

  7. java集合分类

    Java中的集合包括三大类,它们是Set.List和Map, Set(集) List(列表) Map(映射) 它们都处于java.util包中,Set.List和Map都是接口,它们有各自的实现类.( ...

  8. 【Java常用类】Calendar

    Calendar日历类 实例化 方法一 创建其子类**(GregorianCalendar)**的对象 方法二 调用其静态方法getInstance() Calendar instance = Cal ...

  9. Web开发之request

    request常用方法 //常用方法 //得到的是:协议+服务器地址+端口号+工程名称+资源地址+参数 String url = request.getRequestURL(); //得到的是:工程名 ...

  10. Java对象内存模型

    2 Java对象内存模型 在HotSpot虚拟机中,对象在内存中存储的布局可以分为3块区域:对象头(Header). 实例数据(Instance Data)和对齐填充(Padding). 在 JVM ...