Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
J
json-editor
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
IOPSYS
json-editor
Commits
2cdebf74
Commit
2cdebf74
authored
5 years ago
by
Jakob Olsson
Browse files
Options
Downloads
Patches
Plain Diff
add primitive array manipulation
parent
5727c8e5
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
api.c
+119
-32
119 additions, 32 deletions
api.c
with
119 additions
and
32 deletions
api.c
+
119
−
32
View file @
2cdebf74
...
@@ -66,12 +66,87 @@ out:
...
@@ -66,12 +66,87 @@ out:
return
NULL
;
return
NULL
;
}
}
int
get_idx
(
char
*
key
)
{
char
*
open_brace
=
strchr
(
key
,
'['
);
printf
(
"open_brace=%s
\n
"
,
open_brace
);
if
(
open_brace
)
{
char
*
close_brace
=
strchr
(
open_brace
,
']'
);
if
(
close_brace
)
{
int
len
=
close_brace
-
open_brace
;
printf
(
"len=%d
\n
"
,
len
);
char
*
idx_str
=
calloc
(
1
,
len
);
strncpy
(
idx_str
,
open_brace
+
1
,
len
-
1
);
printf
(
"idx_str = %s
\n
"
,
idx_str
);
int
idx
=
atoi
(
idx_str
);
printf
(
"idx = %d
\n
"
,
idx
);
*
open_brace
=
'\0'
;
printf
(
"key=%s
\n
"
,
key
);
return
idx
;
}
}
return
-
2
;
}
int
add_array
(
struct
json_object
*
ptr
,
int
idx
,
char
*
val
,
enum
json_type
type
,
char
*
key
)
{
struct
json_object
*
tmp
;
struct
json_object
*
j_val
;
printf
(
"%s %d
\n
"
,
__func__
,
__LINE__
);
switch
(
type
)
{
case
json_type_array
:
j_val
=
json_tokener_parse
(
val
);
break
;
case
json_type_boolean
:
;;
break
;
case
json_type_object
:
j_val
=
json_tokener_parse
(
val
);
break
;
case
json_type_string
:
j_val
=
json_object_new_string
(
val
);
break
;
case
json_type_double
:
j_val
=
json_object_new_double
(
atof
(
val
));
break
;
case
json_type_int
:
j_val
=
json_object_new_double
(
atoi
(
val
));
break
;
default:
return
-
1
;
break
;
}
printf
(
"%s %d
\n
"
,
__func__
,
__LINE__
);
json_object_object_get_ex
(
ptr
,
key
,
&
tmp
);
if
(
json_object_is_type
(
tmp
,
json_type_array
))
{
if
(
tmp
&&
idx
!=
-
1
)
{
json_object_array_put_idx
(
tmp
,
idx
,
j_val
);
}
else
if
(
tmp
&&
idx
==
-
1
)
{
printf
(
"%s %d
\n
"
,
__func__
,
__LINE__
);
json_object_array_add
(
tmp
,
j_val
);
}
}
else
{
printf
(
"%s %d
\n
"
,
__func__
,
__LINE__
);
tmp
=
json_object_new_array
();
json_object_array_add
(
tmp
,
j_val
);
printf
(
"%s %d type=%s, ptr_type=%s
\n
"
,
__func__
,
__LINE__
,
json_type_to_name
(
json_object_get_type
(
tmp
)),
json_type_to_name
(
json_object_get_type
(
ptr
)));
printf
(
"tmp=%s
\n
"
,
json_object_get_string
(
tmp
));
printf
(
"ptr=%s
\n
"
,
json_object_get_string
(
ptr
));
printf
(
"key=%s
\n
"
,
key
);
json_object_object_add
(
ptr
,
key
,
tmp
);
}
printf
(
"%s %d
\n
"
,
__func__
,
__LINE__
);
return
0
;
}
int
set_by_string
(
char
*
fmt
,
struct
json_object
**
src
,
char
*
val
,
enum
json_type
type
)
int
set_by_string
(
char
*
fmt
,
struct
json_object
**
src
,
char
*
val
,
enum
json_type
type
)
{
{
struct
json_object
*
ptr
,
*
tmp
,
*
tar
;
struct
json_object
*
ptr
,
*
tmp
,
*
tar
;
const
char
*
delimiter
=
"."
;
const
char
*
delimiter
=
"."
;
char
buffer
[
1024
]
=
{
0
};
char
buffer
[
1024
]
=
{
0
};
char
*
p
,
*
prev
=
fmt
;
char
*
p
,
*
prev
=
fmt
;
int
idx
;
if
(
!*
src
)
{
if
(
!*
src
)
{
printf
(
"lets allocate new object
\n
"
);
printf
(
"lets allocate new object
\n
"
);
...
@@ -90,37 +165,29 @@ int set_by_string(char *fmt, struct json_object **src, char *val, enum json_type
...
@@ -90,37 +165,29 @@ int set_by_string(char *fmt, struct json_object **src, char *val, enum json_type
/* if next key exists, parse prev key, let switch-case add/alter last key */
/* if next key exists, parse prev key, let switch-case add/alter last key */
if
(
p
)
{
if
(
p
)
{
json_object_object_get_ex
(
tmp
,
prev
,
&
ptr
);
json_object_object_get_ex
(
tmp
,
prev
,
&
ptr
);
/* if we need to step further and currently marked value isnt an object, we need to overwrite it */
if
(
!
json_object_is_type
(
ptr
,
json_type_object
))
{
ptr
=
json_object_new_object
();
json_object_object_add
(
tmp
,
prev
,
ptr
);
}
printf
(
"p=%s
\n
"
,
p
);
printf
(
"p=%s
\n
"
,
p
);
/* TODO: if prev contains [x], get idx of x */
/* TODO: if prev contains [x], get idx of x */
char
*
open_brace
=
strchr
(
prev
,
'['
);
idx
=
get_idx
(
prev
);
printf
(
"open_brace=%s
\n
"
,
open_brace
);
printf
(
"%s %d
\n
"
,
__func__
,
__LINE__
);
if
(
open_brace
)
{
/* TODO: make enums for rv of get_idx */
char
*
close_brace
=
strchr
(
open_brace
,
']'
);
if
(
idx
!=
-
2
)
{
if
(
close_brace
)
{
if
(
ptr
&&
json_object_get_type
(
ptr
)
==
json_type_array
)
{
int
len
=
close_brace
-
open_brace
;
printf
(
"len=%d
\n
"
,
len
);
char
*
idx_str
=
calloc
(
1
,
len
);
strncpy
(
idx_str
,
open_brace
+
1
,
len
-
1
);
printf
(
"idx_str = %s
\n
"
,
idx_str
);
int
idx
=
atoi
(
idx_str
);
printf
(
"idx = %d
\n
"
,
idx
);
*
open_brace
=
'\0'
;
printf
(
"prev=%s
\n
"
,
prev
);
printf
(
"%s %d
\n
"
,
__func__
,
__LINE__
);
printf
(
"%s %d
\n
"
,
__func__
,
__LINE__
);
if
(
ptr
&&
json_object_get_type
(
ptr
)
==
json_type_array
)
{
ptr
=
json_object_array_get_idx
(
ptr
,
idx
);
printf
(
"%s %d
\n
"
,
__func__
,
__LINE__
);
}
else
{
ptr
=
json_object_array_get_idx
(
ptr
,
idx
);
ptr
=
json_object_new_array
();
}
else
{
printf
(
"%s %d type=%s, ptr_type=%s
\n
"
,
__func__
,
__LINE__
,
json_type_to_name
(
json_object_get_type
(
tmp
)),
json_type_to_name
(
json_object_get_type
(
ptr
)));
ptr
=
json_object_new_array
();
printf
(
"tmp=%s
\n
"
,
json_object_get_string
(
tmp
));
printf
(
"%s %d type=%s, ptr_type=%s
\n
"
,
__func__
,
__LINE__
,
json_type_to_name
(
json_object_get_type
(
tmp
)),
json_type_to_name
(
json_object_get_type
(
ptr
)));
printf
(
"ptr=%s
\n
"
,
json_object_get_string
(
ptr
));
printf
(
"tmp=%s
\n
"
,
json_object_get_string
(
tmp
));
printf
(
"prev=%s
\n
"
,
prev
);
printf
(
"ptr=%s
\n
"
,
json_object_get_string
(
ptr
));
json_object_object_add
(
tmp
,
prev
,
ptr
);
printf
(
"prev=%s
\n
"
,
prev
);
//json_object_array_add(tmp, ptr);
json_object_object_add
(
tmp
,
prev
,
ptr
);
//json_object_array_add(tmp, ptr);
}
}
}
}
}
...
@@ -158,10 +225,23 @@ int set_by_string(char *fmt, struct json_object **src, char *val, enum json_type
...
@@ -158,10 +225,23 @@ int set_by_string(char *fmt, struct json_object **src, char *val, enum json_type
}
}
break
;
break
;
case
json_type_string
:
case
json_type_string
:
json_object_object_add
(
ptr
,
prev
,
json_object_new_string
(
val
));
idx
=
get_idx
(
prev
);
if
(
idx
==
-
2
)
json_object_object_add
(
ptr
,
prev
,
json_object_new_string
(
val
));
else
{
printf
(
"%s %d
\n
"
,
__func__
,
__LINE__
);
add_array
(
ptr
,
idx
,
val
,
type
,
prev
);
}
break
;
break
;
case
json_type_int
:
case
json_type_int
:
json_object_object_add
(
ptr
,
prev
,
json_object_new_int
(
atoi
(
val
)));
printf
(
"%s %d
\n
"
,
__func__
,
__LINE__
);
idx
=
get_idx
(
prev
);
if
(
idx
==
-
2
)
json_object_object_add
(
ptr
,
prev
,
json_object_new_int
(
atoi
(
val
)));
else
{
printf
(
"%s %d
\n
"
,
__func__
,
__LINE__
);
add_array
(
ptr
,
idx
,
val
,
type
,
prev
);
}
break
;
break
;
case
json_type_array
:
case
json_type_array
:
{
{
...
@@ -179,7 +259,14 @@ int set_by_string(char *fmt, struct json_object **src, char *val, enum json_type
...
@@ -179,7 +259,14 @@ int set_by_string(char *fmt, struct json_object **src, char *val, enum json_type
}
}
break
;
break
;
case
json_type_double
:
case
json_type_double
:
json_object_object_add
(
ptr
,
prev
,
json_object_new_int
(
atof
(
val
)));
idx
=
get_idx
(
prev
);
if
(
idx
==
-
2
)
json_object_object_add
(
ptr
,
prev
,
json_object_new_int
(
atof
(
val
)));
else
{
printf
(
"%s %d
\n
"
,
__func__
,
__LINE__
);
add_array
(
ptr
,
idx
,
val
,
type
,
prev
);
}
break
;
break
;
case
json_type_boolean
:
case
json_type_boolean
:
{
{
...
@@ -252,7 +339,7 @@ struct json_object *get(char *fmt, struct json_object *src)
...
@@ -252,7 +339,7 @@ struct json_object *get(char *fmt, struct json_object *src)
int
main
()
int
main
()
{
{
struct
json_object
*
obj
=
path_to_obj
(
"/home/jakob/git/json-editor
-api
/test.json"
);
struct
json_object
*
obj
=
path_to_obj
(
"/home/jakob/git/json-editor/test.json"
);
struct
json_object
*
ptr
;
struct
json_object
*
ptr
;
printf
(
"%s %d
\n
"
,
__func__
,
__LINE__
);
printf
(
"%s %d
\n
"
,
__func__
,
__LINE__
);
...
@@ -269,7 +356,7 @@ int main()
...
@@ -269,7 +356,7 @@ int main()
printf
(
"%s
\n
"
,
json_object_get_string
(
ptr
));
printf
(
"%s
\n
"
,
json_object_get_string
(
ptr
));
set_by_string
(
"test.apdfgj[
1
]"
,
&
obj
,
"1"
,
json_type_string
);
set_by_string
(
"test.apdfgj[
0
]"
,
&
obj
,
"1"
,
json_type_string
);
printf
(
"%s
\n
"
,
json_object_get_string
(
obj
));
printf
(
"%s
\n
"
,
json_object_get_string
(
obj
));
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment