@@ -144,34 +144,51 @@ json_print_array_close(struct jsonpr_ctx *pctx)
144144 * @brief Get the node's module name to use as the @p node prefix in JSON.
145145 *
146146 * @param[in] node Node to process.
147- * @return The name of the module where the @p node belongs, it can be NULL in case the module name
148- * cannot be determined (source format is XML and the refered namespace is unknown/not implemented in the current context) .
147+ * @param[out] mod_name Module name of @p node, can be NULL (format is XML and the module with NS is not in the context).
148+ * @param[out] data_dict Whether @p mod_name is from the schema or data dictionary .
149149 */
150- static const char *
151- node_prefix (const struct lyd_node * node )
150+ static void
151+ node_prefix (const struct lyd_node * node , const char * * mod_name , ly_bool * data_dict )
152152{
153+ struct lyd_node_opaq * onode ;
154+ const struct lys_module * mod ;
155+
156+ * mod_name = NULL ;
157+
158+ if (!node ) {
159+ return ;
160+ }
161+
153162 if (node -> schema ) {
154- return node -> schema -> module -> name ;
163+ * mod_name = node -> schema -> module -> name ;
164+ if (data_dict ) {
165+ * data_dict = 0 ;
166+ }
155167 } else {
156- struct lyd_node_opaq * onode = (struct lyd_node_opaq * )node ;
157- const struct lys_module * mod ;
168+ onode = (struct lyd_node_opaq * )node ;
158169
159170 switch (onode -> format ) {
160171 case LY_VALUE_JSON :
161- return onode -> name .module_name ;
172+ * mod_name = onode -> name .module_name ;
173+ if (data_dict ) {
174+ * data_dict = 1 ;
175+ }
176+ break ;
162177 case LY_VALUE_XML :
163178 mod = ly_ctx_get_module_implemented_ns (onode -> ctx , onode -> name .module_ns );
164- if (!mod ) {
165- return NULL ;
179+ if (mod ) {
180+ * mod_name = mod -> name ;
181+ if (data_dict ) {
182+ * data_dict = 0 ;
183+ }
166184 }
167- return mod -> name ;
185+ break ;
168186 default :
169187 /* cannot be created */
170188 LOGINT (LYD_CTX (node ));
189+ break ;
171190 }
172191 }
173-
174- return NULL ;
175192}
176193
177194/**
@@ -187,6 +204,9 @@ node_prefix(const struct lyd_node *node)
187204int
188205json_nscmp (const struct lyd_node * node1 , const struct lyd_node * node2 )
189206{
207+ const char * pref1 , * pref2 ;
208+ ly_bool dd1 , dd2 ;
209+
190210 assert (node1 || node2 );
191211
192212 if (!node1 || !node2 ) {
@@ -200,10 +220,10 @@ json_nscmp(const struct lyd_node *node1, const struct lyd_node *node2)
200220 return 1 ;
201221 }
202222 } else {
203- const char * pref1 = node_prefix (node1 );
204- const char * pref2 = node_prefix (node2 );
223+ node_prefix (node1 , & pref1 , & dd1 );
224+ node_prefix (node2 , & pref2 , & dd2 );
205225
206- if (( pref1 && pref2 ) && (pref1 == pref2 )) {
226+ if (pref1 && pref2 && ((( dd1 == dd2 ) && (pref1 == pref2 )) || (( dd1 != dd2 ) && ! strcmp ( pref1 , pref2 )) )) {
207227 return 0 ;
208228 } else {
209229 return 1 ;
@@ -271,11 +291,14 @@ json_print_string(struct ly_out *out, const char *text)
271291static LY_ERR
272292json_print_member (struct jsonpr_ctx * pctx , const struct lyd_node * node , ly_bool is_attr )
273293{
294+ const char * pref ;
295+
274296 PRINT_COMMA ;
275297 if ((LEVEL == 1 ) || json_nscmp (node , pctx -> parent )) {
276298 /* print "namespace" */
299+ node_prefix (node , & pref , NULL );
277300 ly_print_ (pctx -> out , "%*s\"%s%s:%s\":%s" , INDENT , is_attr ? "@" : "" ,
278- node_prefix ( node ) , node -> schema -> name , DO_FORMAT ? " " : "" );
301+ pref , node -> schema -> name , DO_FORMAT ? " " : "" );
279302 } else {
280303 ly_print_ (pctx -> out , "%*s\"%s%s\":%s" , INDENT , is_attr ? "@" : "" , node -> schema -> name , DO_FORMAT ? " " : "" );
281304 }
@@ -297,7 +320,8 @@ static LY_ERR
297320json_print_member2 (struct jsonpr_ctx * pctx , const struct lyd_node * parent , LY_VALUE_FORMAT format ,
298321 const struct ly_opaq_name * name , ly_bool is_attr )
299322{
300- const char * module_name = NULL , * name_str ;
323+ const char * module_name = NULL , * name_str , * pmod_name ;
324+ const struct lys_module * mod ;
301325
302326 PRINT_COMMA ;
303327
@@ -307,17 +331,15 @@ json_print_member2(struct jsonpr_ctx *pctx, const struct lyd_node *parent, LY_VA
307331 case LY_VALUE_JSON :
308332 module_name = name -> module_name ;
309333 break ;
310- case LY_VALUE_XML : {
311- const struct lys_module * mod = NULL ;
312-
334+ case LY_VALUE_XML :
335+ mod = NULL ;
313336 if (name -> module_ns ) {
314337 mod = ly_ctx_get_module_implemented_ns (pctx -> ctx , name -> module_ns );
315338 }
316339 if (mod ) {
317340 module_name = mod -> name ;
318341 }
319342 break ;
320- }
321343 default :
322344 /* cannot be created */
323345 LOGINT_RET (pctx -> ctx );
@@ -329,7 +351,8 @@ json_print_member2(struct jsonpr_ctx *pctx, const struct lyd_node *parent, LY_VA
329351 }
330352
331353 /* print the member, strcmp because node prefix is in the schema dict, module_name in data dict */
332- if (module_name && (!parent || strcmp (node_prefix (parent ), module_name ))) {
354+ node_prefix (parent , & pmod_name , NULL );
355+ if (module_name && (!parent || strcmp (pmod_name , module_name ))) {
333356 ly_print_ (pctx -> out , "%*s\"%s%s:%s\":%s" , INDENT , is_attr ? "@" : "" , module_name , name_str , DO_FORMAT ? " " : "" );
334357 } else {
335358 ly_print_ (pctx -> out , "%*s\"%s%s\":%s" , INDENT , is_attr ? "@" : "" , name_str , DO_FORMAT ? " " : "" );
0 commit comments