조회 수 8709 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

Qt로 데이터를 직렬화하는 방법에대해서 소개한다.

 

JSON 유형은 데이터를 직렬화하기에 가장 잘 알려진 접근법일 것이다. Qt는 QJsonObject 와 QJsonDocument 클래스로 JSON 유형으로 작성 후 QByteArray로 직렬화 할 수 있는 방법을 제공한다.

 

JSON의 장점:

  • 데이터를 구조화 할 수 있고 사람이 읽을 수도 있다.
  • 일반 정보 교환이 쉽다.
  • 추가되는 정보에 대해서 메시지를 쉽게 확장 할 수 있다.
  • JSON을 수신하고 파싱하기위한 많은 솔루션이 존재한다.

다음 예제 코드는 센서정보가 QJsonObject 에 저장되고 QJsonDocument 는 값을 QByteArray로 직렬화한다.

#include <QJsonObject>
#include <QJsonDocument>

...

QJsonObject jobject;
jobject["SensorID"] = m_id;
jobject["AmbientTemperature"] = m_ambientTemperature;
jobject["ObjectTemperature"] = m_objectTemperature;
jobject["AccelerometerX"] = m_accelerometerX;
jobject["AccelerometerY"] = m_accelerometerY;
jobject["AccelerometerZ"] = m_accelerometerZ;
jobject["Altitude"] = m_altitude;
jobject["Light"] = m_light;
jobject["Humidity"] = m_humidity;

QJsonDocument doc( jobject );
return doc.toJson();

...

 

이진 JSON은 가독성을 떨어 뜨릴 수 있지만 처리 속도를 높일 수 있는 장점이 있다. doc.toJson() 을 doc.toBinaryData() 로 간단히 전환하면 속도가 높아진다.

 

데이터 흐름(프로토콜)이 결정되어 변경되지 않을 경우 QDataStream 도 권장되는 옵션이다. SensorInformation 클래스에서 이를 지원하려면 두개의 추가 연산자가 필요하다.

QDataStream &operator<<(QDataStream &, const SensorInformation &);
QDataStream &operator>>(QDataStream &, SensorInformation &);

 

구현도 간단하며 아래는 직렬화에 대한 것이다.

QDataStream &operator<<(QDataStream &out, const SensorInformation &item)
{
    QDataStream::FloatingPointPrecision prev = out.floatingPointPrecision();
    out.setFloatingPointPrecision(QDataStream::DoublePrecision);
    out << item.m_id
        << item.m_ambientTemperature
        << item.m_objectTemperature
        << item.m_accelerometerX
        << item.m_accelerometerY
        << item.m_accelerometerZ
        << item.m_altitude
        << item.m_light
        << item.m_humidity;
    out.setFloatingPointPrecision(prev);
    return out;
}

QDataStream &operator>>(QDataStream &in, SensorInformation &item)
{
    QDataStream::FloatingPointPrecision prev = in.floatingPointPrecision();
    in.setFloatingPointPrecision(QDataStream::DoublePrecision);

    in >> item.m_id
            >> item.m_ambientTemperature
            >> item.m_objectTemperature
            >> item.m_accelerometerX
            >> item.m_accelerometerY
            >> item.m_accelerometerZ
            >> item.m_altitude
            >> item.m_light
            >> item.m_humidity;
    in.setFloatingPointPrecision(prev);
    return in;
}

 

QDataStream을 사용하면 텍스트 JSON보다 10 배 더 빠르며 또한 평균 메시지 크기도 대폭 줄어든다. 따라서 QDataStream은 확실히 괜찮은 옵션이다.

 

프로젝트에서 타사 오픈소스를 추가할 수 있는 경우 가장 좋은 직렬화 솔루션 중 하나는 Google의 Protocol Buffers(protobuf)가 있다.


  1. No Image notice

    Qt프로그래밍(QtQuick) Beginner를 위한 글 읽는 순서

    Date2019.01.05 CategoryQML and Qt Quick By운영자 Views436415
    read more
  2. UI 폼(Form)작성 시 탭 순서(Tab Order) 설정

    Date2020.08.24 CategoryGeneral and Desktop Bymakersweb Views14445
    Read More
  3. No Image

    Qt로 데이터를 직렬화(serialization)하는 방법

    Date2020.08.04 CategoryC++ Class Bymakersweb Views8709
    Read More
  4. 최초의 Qt 6.0 스냅샷 제공 (First Qt 6.0 Snapshot Available)

    Date2020.06.21 CategoryQt 6 Byj2doll Views12219
    Read More
  5. Qt MQTT 에 대해서

    Date2020.06.02 CategoryMobile and Embedded Bymakersweb Views11748
    Read More
  6. Embedded Linux 에서 Qt 및 Graphics Stack

    Date2020.05.27 By운영자 Views8419
    Read More
  7. ShaderEffect QML Type을 이용한 버튼 클릭 효과

    Date2020.05.22 CategoryQML and Qt Quick Bymakersweb Views9909
    Read More
  8. Qt기반의 서버와 클라이언트간 SOAP(Simple Object Access Protocol)

    Date2020.05.11 CategoryGeneral and Desktop Bymakersweb Views13813
    Read More
  9. No Image

    재진입(Reentrancy) 및 스레드 안전성(Thread-Safety)

    Date2020.04.19 CategoryC++ Class Bymakersweb Views11122
    Read More
  10. No Image

    Qt 5.15 및 Qt 6의 출시 일정

    Date2020.04.09 CategoryQt 6 Bymakersweb Views10835
    Read More
  11. 콘솔에서 사용자 입력받기

    Date2020.03.22 CategoryGeneral and Desktop Bymakersweb Views60398
    Read More
  12. No Image

    컨테이너 클래스 - QVector

    Date2020.03.17 CategoryC++ Class Bymakersweb Views10616
    Read More
  13. Qt로 작성된 안드로이드 APP에 Splash Screen을 추가

    Date2020.03.10 CategoryMobile and Embedded Bymakersweb Views11639
    Read More
  14. No Image

    QLabel의 텍스트 색과 배경색을 변경

    Date2020.02.25 CategoryC++ Class Bymakersweb Views15530
    Read More
  15. 라즈베리파이4에 대한 Qt 5.14.1 크로스컴파일

    Date2020.02.12 CategoryMobile and Embedded Bymakersweb Views13117
    Read More
  16. QOpenGLWidget 을 투명하게 적용

    Date2020.02.05 CategoryGeneral and Desktop Bymakersweb Views9247
    Read More
  17. No Image

    2020년에 변경되는 Qt 오퍼 (Qt offering changes 2020)

    Date2020.01.31 CategoryGeneral and Desktop Byj2doll Views15296
    Read More
  18. No Image

    Qt로 XML 파싱 : Qt 6에서 업데이트된 (Parsing XML with Qt: Updates for Qt 6)

    Date2020.01.16 CategoryQt 6 Byj2doll Views21336
    Read More
  19. No Image

    Qt 멀티 스레드 프로그래밍 시 유의해야 할 몇 가지

    Date2020.01.13 CategoryGeneral and Desktop Bymakersweb Views13809
    Read More
  20. ApplicationWindow 와 메뉴바(MenuBar)구성

    Date2020.01.04 CategoryQML and Qt Quick Bymakersweb Views15271
    Read More
  21. No Image

    QThread 소개 및 예제

    Date2019.12.25 CategoryC++ Class Bymakersweb Views29025
    Read More
Board Pagination Prev 1 2 3 4 5 6 9 Next
/ 9