簡體   English   中英

gSOAP:如何在C中比較兩個值結構(帶有JSON內容)?

[英]gSOAP: How to compare two value-structs (with JSON-content) in C?

我正在為客戶端應用程序使用gSOAP(用C語言編寫),該應用程序正在調用Java網絡服務。 我正在使用json_call()函數。 我有一個用JSON輸入數據填充的請求結構,我得到了一個用Java服務填充JSON輸出數據的響應結構。 兩種JSON通常具有相同的結構,但是可以具有更多,更少或更改的元素。

我現在的任務是找出響應與請求中哪些元素不同。 主要元素是由許多成員組成的大型數組,例如:

{
    "objects": [
    {
        "@id": "OBJ00001",
        "name": "value",
        ...
    },
    {
        "@id": "OBJ00002",
        "number": 123,
        ...
    },
    ...
    ]
}

我可以使用@id字段來識別任何同類對象。

使用以下方法迭代objects數組很簡單:

for(i = 0; i < has_size(value_at(response, "objects")); i++)

但是然后我缺少了一個函數,該函數可以在請求和響應中比較具有相同@id成員(“對象”)。 類似於“ findMemberWithSameField”,然后是“ equal”( 兩者都不存在! ):

struct member *currentMemberInResponse = NULL;
struct member *memberWithSameField     = NULL;

for(i = 0; i < has_size(value_at(response, "objects")); i++)
{
    /* get the current member out of the response array */
    currentMemberInResponse = nth_value(value_at(response, "objects"), i);

    /* Find member/object with same @id in request */
    memberWithSameField = findMemberWithSameField(value_at(request, "objects"), currentMemberInResponse , "@id"));

    /* equal is true if all fields are the same */
    if(equal(currentMemberInResponse, memberWithSameField))
    {
        /* Do nothing, because nothing changed */
    }
    else
    {
        /* Do something */
    }
}

對這個任務有什么想法嗎? 否則,我必須編寫自己的“ findMemberWithSameField”和“ euqal”。

親切的問候丹尼爾

JSON C ++ API定義了operator==來遞歸比較兩個對象。 最新版本2.8.55(我已經測試過)可以比較兩個JSON對象,其中operator==調用以下函數:

bool json_eqv(const value& x, const value& y)
{
  ...
  switch (x.__type)
  {
    ...
    case SOAP_TYPE__struct:
      if (x.size() != y.size())
        return false;
      else
      {
        const _struct& s = x;
        const _struct& t = y;
        for (_struct::iterator i = s.begin(); i != s.end(); ++i)
        {
          _struct::iterator j;
          for (j = t.begin(); j != t.end(); ++j)
            if (!strcmp(i.name(), j.name()))
              break;
          if (j == t.end() || *i != *j)
            return false;
        }
        return true;
      }

可以使用以下方式將其重寫為C:

int json_eqv(const value *x, const value *y)
{
  if (x->__type != y->__type &&
      (x->__type != SOAP_TYPE__i4 || y->__type != SOAP_TYPE__int) &&
      (x->__type != SOAP_TYPE__int || y->__type != SOAP_TYPE__i4))
    return false;
  switch (x->__type)
  {
    case SOAP_TYPE__boolean:
    case SOAP_TYPE__i4:
    case SOAP_TYPE__int:
      return int_of(x) == int_of(y);
    case SOAP_TYPE__double:
      return double_of(x) == double_of(y);
    case SOAP_TYPE__string:
    case SOAP_TYPE__dateTime_DOTiso8601:
      return !strcmp(string_of(x), string_of(y));
    case SOAP_TYPE__struct:
      if (has_size(x) != has_size(y))
        return 0;
      else
      {
        size_t i, j;
        for (i = 0; i < has_size(x); ++i)
        {
          for (j = 0; j < has_size(y); ++j)
            if (!strcmp(nth_member(x, i), nth_member(y, j))
              break;
          if (j == has_size(y) || !json_eqv(nth_value(x, i), nth_value(y, j))
            return 0;
        }
        return 1;
      }
    case SOAP_TYPE__array:
      if (has_size(x) != has_size(y))
        return 0;
      else
      {
        int i;
        for (i = 0 ; i < has_size(x); ++i)
          if (!json_eqv(nth_nth(x, i), nth_nth(y, i))
            return 0;
        return 1;
      }
    default:
      return 0;
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM