패키지, 저장소 및 앱 카테고리 검색

패키지 상세v0.2.1

rfc_6901

JSON 포인터(RFC 6901). JSON 문서 내 참조된 값을 읽거나 씁니다.

AndroidiOSWebmacOSWindowsLinux
30일 다운로드
89.8만
좋아요
32
Pub 점수
160

패키지 설치

flutter pub add rfc_6901

패키지 개요

공개 정보를 바탕으로 생성한 선택 가이드입니다.

rfc_6901 패키지는 Dart 및 Flutter용 JSON 포인터(RFC 6901)를 구현하여 JSON 문서의 안전한 탐색과 조작을 가능하게 합니다. /foo/0/bar와 같은 포인터 문법을 사용해 중첩된 JSON 구조 내에서 값을 읽고 쓸 수 있습니다. 이는 API, 구성 파일 또는 상태 관리에서 동적 데이터를 처리할 때 특히 유용하며, 전체 구조를 파싱하지 않고도 JSON 필드에 정확히 접근해야 할 경우에 적합합니다. 이 패키지는 모바일, 웹, 데스크톱을 포함한 모든 주요 플랫폼을 지원합니다.

사용 사례

  • 동적으로 중첩된 JSON 값에 액세스하기
  • JSON 문서의 특정 필드 업데이트
  • 변동 구조를 가진 API 응답 파싱
  • 상태 관리에서 부분 업데이트 구현
  • 중첩 키를 사용하는 구성 파일과 함께 작업하기

주요 기능

  • RFC 6901 JSON 포인터 표준을 지원합니다
  • 안전한 값 검색 및 할당
  • 배열과 객체를 균일하게 처리합니다
  • 크로스 플랫폼 호환성
  • 최소 종속성 풋프린트

적합한 대상

  • 동적 JSON 데이터를 다루는 개발자
  • 선택적 필드 접근이 필요한 API 클라이언트
  • 패치 작업이 필요한 상태 관리 시스템
  • 깊은 중첩을 가진 구성 파서

고려 사항

  • 유효한 JSON 포인터 구문이 필요합니다
  • 런타임에 대상 경로를 검증하지 않습니다
  • 기존 JSON에 대한 읽기/쓰기 작업에 제한됨
  • 복잡한 JSON 변환에 적합하지 않습니다

원본 README

연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.

RFC 6901 JSON Pointer

JSON Pointer (RFC 6901) implementation in Dart.

Features:

  • Reads, writes, removes values
  • Fully implements the standard, including the - character
  • Can build the entire path, e.g. /foo/bar/-/baz
  • No external dependencies
  • 100% test coverage

Constructing pointers

A JSON Pointer object can be parsed from a string expression. The expression MUST be a valid non-empty JSON Pointer expression with "/" and "~" escaped.

final pointer = JsonPointer('/foo/0/bar'); // a valid non-empty pointer
final alsoPointer = JsonPointer('/foo/-'); // a valid non-empty pointer with a special "-" reference
final emptyPointer = JsonPointer(); // an empty pointer

JsonPointer('oops'); // throws a FormatException: expression doesn't start with "/"
JsonPointer('/foo/~'); // throws a FormatException: unescaped "~"

It can also be built from individual reference tokens. In such case, it will escape the tokens automatically.

final pointer = JsonPointer.build(['foo', '0', 'bar/']); // Makes "/foo/0/bar~1"
final longerPointer = JsonPointerSegment('-', pointer); // Makes "/foo/0/bar~1/-"
final evenLongerPointer = JsonPointerSegment('wow', longerPointer); // Makes "/foo/0/bar~1/-/wow"

Reading and writing values

The read() method returns the referenced value. The write() method returns a copy of the document with the referenced value replaced by the new value.

import 'dart:convert';

import 'package:rfc_6901/rfc_6901.dart';

void main() {
  const json = '{"foo": [{"bar": 42}]}';
  final document = jsonDecode(json);
  print('Original document: $document');

  [
    '/foo', // reads the array
    '/foo/0', // reads the first element of the array
    '/foo/0/bar', // reads 42
  ].forEach((expression) {
    final pointer = JsonPointer(expression);
    print('Pointer "$pointer" reads ${pointer.read(document)}');
  });

  [
    '/foo/0/bar', // {foo: [{bar: banana}]}
    '/foo/-', // {foo: [{bar: 42}, banana]}
    '/a/b/-/c/d', // {foo: [{bar: 42}], a: {b: [{c: {d: banana}}]}}
  ].forEach((expression) {
    final pointer = JsonPointer(expression);
    final d = pointer.write(document, 'banana');
    print('Add a banana at "$pointer": $d');
  });
}

카테고리

소스 저장소

이 패키지를 사용하는 프로젝트