0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

君、エンジニア名乗ってるのにJSONパーサ書いたことないの??

0
Last updated at Posted at 2026-09-08

「君、エンジニア名乗ってるのにJSONパーサ書いたことないの??」

という声が何処からともなく聞こえてきたので書いた。

この記事から着想を得て、いろんな言語でJSONパーサを実装することにしました。
参考: https://github.com/yubrot/rosetta-lisp

まだまだ始めたばかりでして、進捗としてはC言語が完成、今はHaskellでの実装に着手したばかりです。

機能は、JSONデータ型とそのアクセサ、文字列toJSONデータのパーサ、JSONデータto文字列のジェネレータ。仕様はRFC 8259に準拠してます。

Cでのデータ型はこんな感じ。

typedef enum e_json_type
{
	JSON_Null,
	JSON_True,
	JSON_False,
	JSON_Number,
	JSON_String,
	JSON_Array,
	JSON_Object
}	t_json_type;

typedef enum e_json_error_code
{
	NO_ERROR,
	FAILED_TO_MEMORY_ALLOCATION,
	INVALID_TOKEN,
	NESTING_IS_TOO_DEEP,
	TRAILING_GARBAGE
}	t_error_code;

typedef struct s_json_array
{
	struct s_json_array	*prev;
	struct s_json_array	*next;
	struct s_json		*element;
}	t_json_array;

typedef struct s_json_object
{
	struct s_json_object	*prev;
	struct s_json_object	*next;
	char					*key;
	struct s_json			*value;
}	t_json_object;

typedef struct s_json
{
	t_json_type		type;
	union u_json_data
	{
		bool			bool_data;
		double			number_data;
		char			*string_data;
		t_json_array	*array_data;
		t_json_object	*object_data;
	}	_;
}	t_json;

パーサ実装は再帰下降解析を使ってます。

目標は10言語くらいで実装しきること。
一応学習目的なのでAIの使用はかなり控えめでコツコツやってます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?