diff options
Diffstat (limited to 'src/include/gnunet_json_lib.h')
-rw-r--r-- | src/include/gnunet_json_lib.h | 304 |
1 files changed, 304 insertions, 0 deletions
diff --git a/src/include/gnunet_json_lib.h b/src/include/gnunet_json_lib.h index 3dc79105e..92f696e08 100644 --- a/src/include/gnunet_json_lib.h +++ b/src/include/gnunet_json_lib.h | |||
@@ -521,6 +521,310 @@ GNUNET_JSON_getopt (char shortName, | |||
521 | const char *description, | 521 | const char *description, |
522 | json_t **json); | 522 | json_t **json); |
523 | 523 | ||
524 | |||
525 | /* ****************** JSON PACK helper ******************* */ | ||
526 | |||
527 | |||
528 | /** | ||
529 | * Element in the array to give to the packer. | ||
530 | */ | ||
531 | struct GNUNET_JSON_PackSpec; | ||
532 | |||
533 | |||
534 | /** | ||
535 | * Function called to pack an element into the JSON | ||
536 | * object as part of #GNUNET_JSON_pack_(). | ||
537 | * | ||
538 | * @param se pack specification to execute | ||
539 | * @return json object to pack, NULL to pack nothing | ||
540 | */ | ||
541 | typedef json_t * | ||
542 | (*GNUNET_JSON_PackCallback)(const struct GNUNET_JSON_PackSpec *se); | ||
543 | |||
544 | |||
545 | /** | ||
546 | * Element in the array to give to the packer. | ||
547 | */ | ||
548 | struct GNUNET_JSON_PackSpec | ||
549 | { | ||
550 | /** | ||
551 | * Name of the field to pack. | ||
552 | */ | ||
553 | const char *field_name; | ||
554 | |||
555 | /** | ||
556 | * Object to pack. | ||
557 | */ | ||
558 | json_t *object; | ||
559 | |||
560 | /** | ||
561 | * True if a NULL (or 0) argument is allowed. In this | ||
562 | * case, if the argument is NULL the @e packer should | ||
563 | * return NULL and the field should be skipped (omitted from | ||
564 | * the generated object) and not be serialized at all. | ||
565 | */ | ||
566 | bool allow_null; | ||
567 | }; | ||
568 | |||
569 | |||
570 | /** | ||
571 | * Pack a JSON object from a @a spec. Aborts if | ||
572 | * packing fails. | ||
573 | * | ||
574 | * @param spec specification object | ||
575 | * @return JSON object | ||
576 | */ | ||
577 | json_t * | ||
578 | GNUNET_JSON_pack_ (struct GNUNET_JSON_PackSpec spec[]); | ||
579 | |||
580 | |||
581 | /** | ||
582 | * Pack a JSON object from a @a spec. Aborts if | ||
583 | * packing fails. | ||
584 | * | ||
585 | * @param ... list of specification objects | ||
586 | * @return JSON object | ||
587 | */ | ||
588 | #define GNUNET_JSON_PACK(...) \ | ||
589 | GNUNET_JSON_pack_ ((struct GNUNET_JSON_PackSpec[]) {__VA_ARGS__, \ | ||
590 | GNUNET_JSON_pack_end_ ()}) | ||
591 | |||
592 | |||
593 | /** | ||
594 | * Do not use directly. Use #GNUNET_JSON_PACK. | ||
595 | * | ||
596 | * @return array terminator | ||
597 | */ | ||
598 | struct GNUNET_JSON_PackSpec | ||
599 | GNUNET_JSON_pack_end_ (void); | ||
600 | |||
601 | |||
602 | /** | ||
603 | * Modify packer instruction to allow NULL as a value. | ||
604 | * | ||
605 | * @param in json pack specification to modify | ||
606 | * @return json pack specification | ||
607 | */ | ||
608 | struct GNUNET_JSON_PackSpec | ||
609 | GNUNET_JSON_pack_allow_null (struct GNUNET_JSON_PackSpec in); | ||
610 | |||
611 | |||
612 | /** | ||
613 | * Generate packer instruction for a JSON field of type | ||
614 | * bool. | ||
615 | * | ||
616 | * @param name name of the field to add to the object | ||
617 | * @param b boolean value | ||
618 | * @return json pack specification | ||
619 | */ | ||
620 | struct GNUNET_JSON_PackSpec | ||
621 | GNUNET_JSON_pack_bool (const char *name, | ||
622 | bool b); | ||
623 | |||
624 | |||
625 | /** | ||
626 | * Generate packer instruction for a JSON field of type | ||
627 | * string. | ||
628 | * | ||
629 | * @param name name of the field to add to the object | ||
630 | * @param s string value | ||
631 | * @return json pack specification | ||
632 | */ | ||
633 | struct GNUNET_JSON_PackSpec | ||
634 | GNUNET_JSON_pack_string (const char *name, | ||
635 | const char *s); | ||
636 | |||
637 | |||
638 | /** | ||
639 | * Generate packer instruction for a JSON field of type | ||
640 | * unsigned integer. Note that the maximum allowed | ||
641 | * value is still limited by JSON and not UINT64_MAX. | ||
642 | * | ||
643 | * @param name name of the field to add to the object | ||
644 | * @param num numeric value | ||
645 | * @return json pack specification | ||
646 | */ | ||
647 | struct GNUNET_JSON_PackSpec | ||
648 | GNUNET_JSON_pack_uint64 (const char *name, | ||
649 | uint64_t num); | ||
650 | |||
651 | |||
652 | /** | ||
653 | * Generate packer instruction for a JSON field of type | ||
654 | * signed integer. | ||
655 | * | ||
656 | * @param name name of the field to add to the object | ||
657 | * @param num numeric value | ||
658 | * @return json pack specification | ||
659 | */ | ||
660 | struct GNUNET_JSON_PackSpec | ||
661 | GNUNET_JSON_pack_int64 (const char *name, | ||
662 | int64_t num); | ||
663 | |||
664 | |||
665 | /** | ||
666 | * Generate packer instruction for a JSON field of type | ||
667 | * JSON object where the reference is taken over by | ||
668 | * the packer. | ||
669 | * | ||
670 | * @param name name of the field to add to the object | ||
671 | * @param o object to steal | ||
672 | * @return json pack specification | ||
673 | */ | ||
674 | struct GNUNET_JSON_PackSpec | ||
675 | GNUNET_JSON_pack_object_steal (const char *name, | ||
676 | json_t *o); | ||
677 | |||
678 | |||
679 | /** | ||
680 | * Generate packer instruction for a JSON field of type JSON object where the | ||
681 | * reference counter is incremented by the packer. Note that a deep copy is | ||
682 | * not performed. | ||
683 | * | ||
684 | * @param name name of the field to add to the object | ||
685 | * @param o object to increment reference counter of | ||
686 | * @return json pack specification | ||
687 | */ | ||
688 | struct GNUNET_JSON_PackSpec | ||
689 | GNUNET_JSON_pack_object_incref (const char *name, | ||
690 | json_t *o); | ||
691 | |||
692 | |||
693 | /** | ||
694 | * Generate packer instruction for a JSON field of type | ||
695 | * JSON array where the reference is taken over by | ||
696 | * the packer. | ||
697 | * | ||
698 | * @param name name of the field to add to the object | ||
699 | * @param a array to steal | ||
700 | * @return json pack specification | ||
701 | */ | ||
702 | struct GNUNET_JSON_PackSpec | ||
703 | GNUNET_JSON_pack_array_steal (const char *name, | ||
704 | json_t *a); | ||
705 | |||
706 | |||
707 | /** | ||
708 | * Generate packer instruction for a JSON field of type JSON array where the | ||
709 | * reference counter is incremented by the packer. Note that a deep copy is | ||
710 | * not performed. | ||
711 | * | ||
712 | * @param name name of the field to add to the object | ||
713 | * @param a array to increment reference counter of | ||
714 | * @return json pack specification | ||
715 | */ | ||
716 | struct GNUNET_JSON_PackSpec | ||
717 | GNUNET_JSON_pack_array_incref (const char *name, | ||
718 | json_t *a); | ||
719 | |||
720 | |||
721 | /** | ||
722 | * Generate packer instruction for a JSON field of type | ||
723 | * variable size binary blob. | ||
724 | * | ||
725 | * @param name name of the field to add to the object | ||
726 | * @param blob binary data to pack | ||
727 | * @param blob_size number of bytes in @a blob | ||
728 | * @return json pack specification | ||
729 | */ | ||
730 | struct GNUNET_JSON_PackSpec | ||
731 | GNUNET_JSON_pack_data_varsize (const char *name, | ||
732 | const void *blob, | ||
733 | size_t blob_size); | ||
734 | |||
735 | |||
736 | /** | ||
737 | * Generate packer instruction for a JSON field where the | ||
738 | * size is automatically determined from the argument. | ||
739 | * | ||
740 | * @param name name of the field to add to the object | ||
741 | * @param blob data to pack, must not be an array | ||
742 | * @return json pack specification | ||
743 | */ | ||
744 | #define GNUNET_JSON_pack_data_auto(name,blob) \ | ||
745 | GNUNET_JSON_pack_data_varsize (name, blob, sizeof (*blob)) | ||
746 | |||
747 | |||
748 | /** | ||
749 | * Generate packer instruction for a JSON field of type | ||
750 | * absolute time. | ||
751 | * | ||
752 | * @param name name of the field to add to the object | ||
753 | * @param at absolute time to pack, a value of 0 is only | ||
754 | * allowed with #GNUNET_JSON_pack_allow_null()! | ||
755 | * @return json pack specification | ||
756 | */ | ||
757 | struct GNUNET_JSON_PackSpec | ||
758 | GNUNET_JSON_pack_time_abs (const char *name, | ||
759 | struct GNUNET_TIME_Absolute at); | ||
760 | |||
761 | |||
762 | /** | ||
763 | * Generate packer instruction for a JSON field of type | ||
764 | * absolute time in network byte order. | ||
765 | * | ||
766 | * @param name name of the field to add to the object | ||
767 | * @param at absolute time to pack, a value of 0 is only | ||
768 | * allowed with #GNUNET_JSON_pack_allow_null()! | ||
769 | * @return json pack specification | ||
770 | */ | ||
771 | struct GNUNET_JSON_PackSpec | ||
772 | GNUNET_JSON_pack_time_abs_nbo (const char *name, | ||
773 | struct GNUNET_TIME_AbsoluteNBO at); | ||
774 | |||
775 | |||
776 | /** | ||
777 | * Generate packer instruction for a JSON field of type | ||
778 | * relative time. | ||
779 | * | ||
780 | * @param name name of the field to add to the object | ||
781 | * @param rt relative time to pack | ||
782 | * @return json pack specification | ||
783 | */ | ||
784 | struct GNUNET_JSON_PackSpec | ||
785 | GNUNET_JSON_pack_time_rel (const char *name, | ||
786 | struct GNUNET_TIME_Relative rt); | ||
787 | |||
788 | |||
789 | /** | ||
790 | * Generate packer instruction for a JSON field of type | ||
791 | * relative time in network byte order. | ||
792 | * | ||
793 | * @param name name of the field to add to the object | ||
794 | * @param rt relative time to pack | ||
795 | * @return json pack specification | ||
796 | */ | ||
797 | struct GNUNET_JSON_PackSpec | ||
798 | GNUNET_JSON_pack_time_rel_nbo (const char *name, | ||
799 | struct GNUNET_TIME_RelativeNBO rt); | ||
800 | |||
801 | |||
802 | /** | ||
803 | * Generate packer instruction for a JSON field of type | ||
804 | * RSA public key. | ||
805 | * | ||
806 | * @param name name of the field to add to the object | ||
807 | * @param pk RSA public key | ||
808 | * @return json pack specification | ||
809 | */ | ||
810 | struct GNUNET_JSON_PackSpec | ||
811 | GNUNET_JSON_pack_rsa_public_key (const char *name, | ||
812 | const struct GNUNET_CRYPTO_RsaPublicKey *pk); | ||
813 | |||
814 | |||
815 | /** | ||
816 | * Generate packer instruction for a JSON field of type | ||
817 | * RSA signature. | ||
818 | * | ||
819 | * @param name name of the field to add to the object | ||
820 | * @param sig RSA signature | ||
821 | * @return json pack specification | ||
822 | */ | ||
823 | struct GNUNET_JSON_PackSpec | ||
824 | GNUNET_JSON_pack_rsa_signature (const char *name, | ||
825 | const struct GNUNET_CRYPTO_RsaSignature *sig); | ||
826 | |||
827 | |||
524 | #endif | 828 | #endif |
525 | 829 | ||
526 | /* end of gnunet_json_lib.h */ | 830 | /* end of gnunet_json_lib.h */ |