json_mel/
value.rs

1use super::*;
2use melodium_macro::{check, mel_function, mel_treatment};
3
4/// Return JSON `null` value.
5#[mel_function]
6pub fn null() -> Json {
7    Json(serde_json::Value::Null)
8}
9
10/// Makes stream of JSON `null` values.
11#[mel_treatment(
12    input ticks Stream<void>
13    output nulls Stream<Json>
14)]
15pub async fn null() {
16    while let Ok(ticks) = ticks.recv_many().await {
17        check!(
18            nulls
19                .send_many(TransmissionValue::Other(
20                    vec![Value::Data(Arc::new(Json(serde_json::Value::Null))); ticks.len()].into()
21                ))
22                .await
23        )
24    }
25}
26
27/// Makes a JSON boolean value.
28#[mel_function(
29    generic B (ToBool)
30)]
31pub fn from_bool(value: B) -> Json {
32    Json(serde_json::Value::Bool(value.to_bool()))
33}
34
35/// Turns stream of boolean convertible values into JSON booleans.
36#[mel_treatment(
37    generic B (ToBool)
38    input value Stream<B>
39    output json Stream<Json>
40)]
41pub async fn from_bool() {
42    while let Ok(values) = value
43        .recv_many()
44        .await
45        .map(|values| Into::<VecDeque<Value>>::into(values))
46    {
47        check!(
48            json.send_many(TransmissionValue::Other(
49                values
50                    .into_iter()
51                    .map(|val| Value::Data(Arc::new(Json(serde_json::Value::Bool(val.to_bool())))))
52                    .collect()
53            ))
54            .await
55        )
56    }
57}
58
59/// Makes a JSON numeric value from i64
60#[mel_function(
61    generic I (ToI64)
62)]
63pub fn from_number_i64(value: I) -> Json {
64    Json(serde_json::Value::from(value.to_i64()))
65}
66
67/// Turns stream of i64 convertible values into JSON numbers.
68#[mel_treatment(
69    generic I (ToI64)
70    input value Stream<I>
71    output json Stream<Json>
72)]
73pub async fn from_number_i64() {
74    while let Ok(values) = value
75        .recv_many()
76        .await
77        .map(|values| Into::<VecDeque<Value>>::into(values))
78    {
79        check!(
80            json.send_many(TransmissionValue::Other(
81                values
82                    .into_iter()
83                    .map(|val| Value::Data(Arc::new(Json(serde_json::Value::from(val.to_i16())))))
84                    .collect()
85            ))
86            .await
87        )
88    }
89}
90
91/// Makes a JSON numeric value from u64
92#[mel_function(
93    generic U (ToU64)
94)]
95pub fn from_number_u64(value: U) -> Json {
96    Json(serde_json::Value::from(value.to_u64()))
97}
98
99/// Turns stream of u64 convertible values into JSON numbers.
100#[mel_treatment(
101    generic U (ToU64)
102    input value Stream<U>
103    output json Stream<Json>
104)]
105pub async fn from_number_u64() {
106    while let Ok(values) = value
107        .recv_many()
108        .await
109        .map(|values| Into::<VecDeque<Value>>::into(values))
110    {
111        check!(
112            json.send_many(TransmissionValue::Other(
113                values
114                    .into_iter()
115                    .map(|val| Value::Data(Arc::new(Json(serde_json::Value::from(val.to_u16())))))
116                    .collect()
117            ))
118            .await
119        )
120    }
121}
122
123/// Try to make a JSON numeric value from f64
124///
125/// An infinite or NaN number is not a valid JSON value, and then return none value if in that case.
126#[mel_function(
127    generic F (ToF64)
128)]
129pub fn try_from_number_f64(value: F) -> Option<Json> {
130    if let Some(num) = serde_json::Number::from_f64(value.to_f64()) {
131        Some(Json(serde_json::Value::from(num)))
132    } else {
133        None
134    }
135}
136
137/// Turns stream of f64 convertible values into JSON numbers.
138///
139/// An infinite or NaN number is not a valid JSON value, and then stream none value if in that case.
140#[mel_treatment(
141    generic F (ToF64)
142    input value Stream<F>
143    output json Stream<Option<Json>>
144)]
145pub async fn try_from_number_f64() {
146    while let Ok(values) = value
147        .recv_many()
148        .await
149        .map(|values| Into::<VecDeque<Value>>::into(values))
150    {
151        check!(
152            json.send_many(TransmissionValue::Other(
153                values
154                    .into_iter()
155                    .map(
156                        |val| if let Some(num) = serde_json::Number::from_f64(val.to_f64()) {
157                            Value::Option(Some(Box::new(Value::Data(Arc::new(Json(
158                                serde_json::Value::from(num),
159                            ))))))
160                        } else {
161                            Value::Option(None)
162                        }
163                    )
164                    .collect()
165            ))
166            .await
167        )
168    }
169}
170
171/// Makes a JSON numeric value from f64
172///
173/// An infinite or NaN number is not a valid JSON value, and then return `replacement` value if in that case.
174#[mel_function(
175    generic F (ToF64)
176)]
177pub fn from_number_f64(value: F, replacement: Json) -> Json {
178    if let Some(num) = serde_json::Number::from_f64(value.to_f64()) {
179        Json(serde_json::Value::from(num))
180    } else {
181        replacement
182    }
183}
184
185/// Turns stream of u64 convertible values into JSON numbers.
186///
187/// An infinite or NaN number is not a valid JSON value, and then stream `replacement` value if in that case.
188#[mel_treatment(
189    generic F (ToF64)
190    input value Stream<F>
191    output json Stream<Json>
192)]
193pub async fn from_number_f64(replacement: Json) {
194    while let Ok(values) = value
195        .recv_many()
196        .await
197        .map(|values| Into::<VecDeque<Value>>::into(values))
198    {
199        check!(
200            json.send_many(TransmissionValue::Other(
201                values
202                    .into_iter()
203                    .map(
204                        |val| if let Some(num) = serde_json::Number::from_f64(val.to_f64()) {
205                            Value::Data(Arc::new(Json(serde_json::Value::from(num))))
206                        } else {
207                            Value::Data(Arc::clone(&replacement) as Arc<dyn Data>)
208                        }
209                    )
210                    .collect()
211            ))
212            .await
213        )
214    }
215}
216
217/// Makes a JSON string value
218#[mel_function(
219    generic S (ToString)
220)]
221pub fn from_string(value: S) -> Json {
222    Json(serde_json::Value::from(DataTrait::to_string(&value)))
223}
224
225/// Turns stream of string convertible values into JSON strings.
226#[mel_treatment(
227    generic S (ToString)
228    input value Stream<S>
229    output json Stream<Json>
230)]
231pub async fn from_string() {
232    while let Ok(values) = value
233        .recv_many()
234        .await
235        .map(|values| Into::<VecDeque<Value>>::into(values))
236    {
237        check!(
238            json.send_many(TransmissionValue::Other(
239                values
240                    .into_iter()
241                    .map(|val| Value::Data(Arc::new(Json(serde_json::Value::String(
242                        DataTrait::to_string(&val)
243                    )))))
244                    .collect()
245            ))
246            .await
247        )
248    }
249}
250
251/// Makes a JSON boolean or null value.
252///
253/// If `value` is some boolean, it is turned into JSON boolean, else if `value` is `none`, `null` JSON value is returned.
254#[mel_function(
255    generic B (ToBool)
256)]
257pub fn from_option_bool(value: Option<B>) -> Json {
258    if let Some(val) = value {
259        Json(serde_json::Value::Bool(val.to_bool()))
260    } else {
261        Json(serde_json::Value::Null)
262    }
263}
264
265/// Turns stream of boolean convertible option values into JSON boolean or null values.
266///
267/// When `value` is some boolean, it is turned into JSON boolean, else if `value` is `none`, `null` JSON value is streamed.
268#[mel_treatment(
269    generic B (ToBool)
270    input value Stream<Option<B>>
271    output json Stream<Json>
272)]
273pub async fn from_option_bool() {
274    while let Ok(values) = value
275        .recv_many()
276        .await
277        .map(|values| Into::<VecDeque<Value>>::into(values))
278    {
279        check!(
280            json.send_many(TransmissionValue::Other(
281                values
282                    .into_iter()
283                    .map(|val| Value::Data(Arc::new(Json(match val {
284                        Value::Option(Some(val)) => {
285                            serde_json::Value::Bool(val.to_bool())
286                        }
287                        _ => serde_json::Value::Null,
288                    }))))
289                    .collect()
290            ))
291            .await
292        )
293    }
294}
295
296/// Makes a JSON numeric or null value from option of convertible i64 value.
297///
298/// If `value` is some number, it is turned into JSON, else if `value` is `none`, `null` JSON value is returned.
299#[mel_function(
300    generic I (ToI64)
301)]
302pub fn from_option_number_i64(value: Option<I>) -> Json {
303    if let Some(val) = value {
304        Json(serde_json::Value::from(val.to_i64()))
305    } else {
306        Json(serde_json::Value::Null)
307    }
308}
309
310/// Turns stream of i64 convertible option values into JSON numbers.
311///
312/// When `value` is some number, it is turned into JSON, else if `value` is `none`, `null` JSON value is streamed.
313#[mel_treatment(
314    generic I (ToI64)
315    input value Stream<Option<I>>
316    output json Stream<Json>
317)]
318pub async fn from_option_number_i64() {
319    while let Ok(values) = value
320        .recv_many()
321        .await
322        .map(|values| Into::<VecDeque<Value>>::into(values))
323    {
324        check!(
325            json.send_many(TransmissionValue::Other(
326                values
327                    .into_iter()
328                    .map(|val| Value::Data(Arc::new(Json(match val {
329                        Value::Option(Some(val)) => {
330                            serde_json::Value::from(val.to_i64())
331                        }
332                        _ => serde_json::Value::Null,
333                    }))))
334                    .collect()
335            ))
336            .await
337        )
338    }
339}
340
341/// Makes a JSON numeric or null value from option of convertible u64 value.
342///
343/// If `value` is some number, it is turned into JSON, else if `value` is `none`, `null` JSON value is returned.
344#[mel_function(
345    generic U (ToU64)
346)]
347pub fn from_option_number_u64(value: Option<U>) -> Json {
348    if let Some(val) = value {
349        Json(serde_json::Value::from(val.to_u64()))
350    } else {
351        Json(serde_json::Value::Null)
352    }
353}
354
355/// Turns stream of u64 convertible option values into JSON numbers.
356///
357/// When `value` is some number, it is turned into JSON, else if `value` is `none`, `null` JSON value is streamed.
358#[mel_treatment(
359    generic U (ToU64)
360    input value Stream<Option<U>>
361    output json Stream<Json>
362)]
363pub async fn from_option_number_u64() {
364    while let Ok(values) = value
365        .recv_many()
366        .await
367        .map(|values| Into::<VecDeque<Value>>::into(values))
368    {
369        check!(
370            json.send_many(TransmissionValue::Other(
371                values
372                    .into_iter()
373                    .map(|val| Value::Data(Arc::new(Json(match val {
374                        Value::Option(Some(val)) => {
375                            serde_json::Value::from(val.to_u64())
376                        }
377                        _ => serde_json::Value::Null,
378                    }))))
379                    .collect()
380            ))
381            .await
382        )
383    }
384}
385
386/// Try to make a JSON numeric value from option f64 convertible value
387///
388/// If `value` is some number, it is turned into JSON, else if `value` is `none`, `null` JSON value is returned.
389/// An infinite or NaN number is not a valid JSON value, and then return none value in that case.
390#[mel_function(
391    generic F (ToF64)
392)]
393pub fn try_from_option_number_f64(value: Option<F>) -> Option<Json> {
394    if let Some(val) = value {
395        if let Some(num) = serde_json::Number::from_f64(val.to_f64()) {
396            Some(Json(serde_json::Value::from(num)))
397        } else {
398            None
399        }
400    } else {
401        Some(Json(serde_json::Value::Null))
402    }
403}
404
405/// Turns stream of option f64 convertible values into JSON numbers.
406///
407/// If `value` is some number, it is turned into JSON, else if `value` is `none`, `null` JSON value is streamed.
408/// An infinite or NaN number is not a valid JSON value, and then stream none value if in that case.
409#[mel_treatment(
410    generic F (ToF64)
411    input value Stream<Option<F>>
412    output json Stream<Option<Json>>
413)]
414pub async fn try_from_option_number_f64() {
415    while let Ok(values) = value
416        .recv_many()
417        .await
418        .map(|values| Into::<VecDeque<Value>>::into(values))
419    {
420        check!(
421            json.send_many(TransmissionValue::Other(
422                values
423                    .into_iter()
424                    .map(|val| match val {
425                        Value::Option(Some(val)) => {
426                            if let Some(num) = serde_json::Number::from_f64(val.to_f64()) {
427                                Value::Option(Some(Box::new(Value::Data(Arc::new(Json(
428                                    serde_json::Value::from(num),
429                                ))))))
430                            } else {
431                                Value::Option(None)
432                            }
433                        }
434                        _ => Value::Option(Some(Box::new(Value::Data(Arc::new(Json(
435                            serde_json::Value::Null
436                        )))))),
437                    })
438                    .collect()
439            ))
440            .await
441        )
442    }
443}
444
445/// Makes a JSON numeric value from option f64 convertible value
446///
447/// If `value` is some number, it is turned into JSON, else if `value` is `none`, `null` JSON value is returned.
448/// An infinite or NaN number is not a valid JSON value, then `replacement` value is used in that case.
449#[mel_function(
450    generic F (ToF64)
451)]
452pub fn from_option_number_f64(value: Option<F>, replacement: Json) -> Json {
453    if let Some(val) = value {
454        if let Some(num) = serde_json::Number::from_f64(val.to_f64()) {
455            Json(serde_json::Value::from(num))
456        } else {
457            replacement
458        }
459    } else {
460        Json(serde_json::Value::Null)
461    }
462}
463
464/// Turns stream of option u64 convertible values into JSON numbers.
465///
466/// If `value` is some number, it is turned into JSON, else if `value` is `none`, `null` JSON value is streamed.
467/// An infinite or NaN number is not a valid JSON value, and then stream `replacement` value if in that case.
468#[mel_treatment(
469    generic F (ToF64)
470    input value Stream<Option<F>>
471    output json Stream<Json>
472)]
473pub async fn from_option_number_f64(replacement: Json) {
474    while let Ok(values) = value
475        .recv_many()
476        .await
477        .map(|values| Into::<VecDeque<Value>>::into(values))
478    {
479        check!(
480            json.send_many(TransmissionValue::Other(
481                values
482                    .into_iter()
483                    .map(|val| match val {
484                        Value::Option(Some(val)) => {
485                            if let Some(num) = serde_json::Number::from_f64(val.to_f64()) {
486                                Value::Data(Arc::new(Json(serde_json::Value::from(num))))
487                            } else {
488                                Value::Data(Arc::clone(&replacement) as Arc<dyn Data>)
489                            }
490                        }
491                        _ => Value::Data(Arc::new(Json(serde_json::Value::Null))),
492                    })
493                    .collect()
494            ))
495            .await
496        )
497    }
498}
499
500/// Makes a JSON string or null value.
501///
502/// If `value` is some string, it is turned into JSON string, else if `value` is `none`, `null` JSON value is returned.
503#[mel_function(
504    generic S (ToString)
505)]
506pub fn from_option_string(value: Option<S>) -> Json {
507    if let Some(val) = value {
508        Json(serde_json::Value::String(DataTrait::to_string(&val)))
509    } else {
510        Json(serde_json::Value::Null)
511    }
512}
513
514/// Turns stream of string convertible option values into JSON string or null values.
515///
516/// When `value` is some string, it is turned into JSON string, else if `value` is `none`, `null` JSON value is streamed.
517#[mel_treatment(
518    generic S (ToString)
519    input value Stream<Option<S>>
520    output json Stream<Json>
521)]
522pub async fn from_option_string() {
523    while let Ok(values) = value
524        .recv_many()
525        .await
526        .map(|values| Into::<VecDeque<Value>>::into(values))
527    {
528        check!(
529            json.send_many(TransmissionValue::Other(
530                values
531                    .into_iter()
532                    .map(|val| Value::Data(Arc::new(Json(match val {
533                        Value::Option(Some(val)) => {
534                            serde_json::Value::String(DataTrait::to_string(&*val))
535                        }
536                        _ => serde_json::Value::Null,
537                    }))))
538                    .collect()
539            ))
540            .await
541        )
542    }
543}
544
545/// Tells if JSON value is null.
546#[mel_function]
547pub fn is_null(value: Json) -> bool {
548    value.0.is_null()
549}
550
551/// Determine if streamed JSON values are null.
552#[mel_treatment(
553    input value Stream<Json>
554    output is_null Stream<bool>
555)]
556pub async fn is_null() {
557    while let Ok(values) = value
558        .recv_many()
559        .await
560        .map(|values| Into::<VecDeque<Value>>::into(values))
561    {
562        check!(
563            is_null
564                .send_many(TransmissionValue::Bool(
565                    values
566                        .into_iter()
567                        .map(|val| match val {
568                            Value::Data(val) => val
569                                .downcast_arc::<Json>()
570                                .map(|json| json.0.is_null())
571                                .unwrap_or(false),
572                            _ => false,
573                        })
574                        .collect()
575                ))
576                .await
577        )
578    }
579}
580
581/// Tells if JSON value is boolean.
582#[mel_function]
583pub fn is_bool(value: Json) -> bool {
584    value.0.is_boolean()
585}
586
587/// Determine if streamed JSON values are booleans.
588#[mel_treatment(
589    input value Stream<Json>
590    output is_bool Stream<bool>
591)]
592pub async fn is_bool() {
593    while let Ok(values) = value
594        .recv_many()
595        .await
596        .map(|values| Into::<VecDeque<Value>>::into(values))
597    {
598        check!(
599            is_bool
600                .send_many(TransmissionValue::Bool(
601                    values
602                        .into_iter()
603                        .map(|val| match val {
604                            Value::Data(val) => val
605                                .downcast_arc::<Json>()
606                                .map(|json| json.0.is_boolean())
607                                .unwrap_or(false),
608                            _ => false,
609                        })
610                        .collect()
611                ))
612                .await
613        )
614    }
615}
616
617/// Tells if JSON value is a string.
618#[mel_function]
619pub fn is_string(value: Json) -> bool {
620    value.0.is_string()
621}
622
623/// Determine if streamed JSON values are strings.
624#[mel_treatment(
625    input value Stream<Json>
626    output is_string Stream<bool>
627)]
628pub async fn is_string() {
629    while let Ok(values) = value
630        .recv_many()
631        .await
632        .map(|values| Into::<VecDeque<Value>>::into(values))
633    {
634        check!(
635            is_string
636                .send_many(TransmissionValue::Bool(
637                    values
638                        .into_iter()
639                        .map(|val| match val {
640                            Value::Data(val) => val
641                                .downcast_arc::<Json>()
642                                .map(|json| json.0.is_string())
643                                .unwrap_or(false),
644                            _ => false,
645                        })
646                        .collect()
647                ))
648                .await
649        )
650    }
651}
652
653/// Tells if JSON value is a number.
654#[mel_function]
655pub fn is_number(value: Json) -> bool {
656    value.0.is_number()
657}
658
659/// Determine if streamed JSON values are numbers.
660#[mel_treatment(
661    input value Stream<Json>
662    output is_number Stream<bool>
663)]
664pub async fn is_number() {
665    while let Ok(values) = value
666        .recv_many()
667        .await
668        .map(|values| Into::<VecDeque<Value>>::into(values))
669    {
670        check!(
671            is_number
672                .send_many(TransmissionValue::Bool(
673                    values
674                        .into_iter()
675                        .map(|val| match val {
676                            Value::Data(val) => val
677                                .downcast_arc::<Json>()
678                                .map(|json| json.0.is_number())
679                                .unwrap_or(false),
680                            _ => false,
681                        })
682                        .collect()
683                ))
684                .await
685        )
686    }
687}
688
689/// Tells if JSON value is a i64 number.
690#[mel_function]
691pub fn is_i64(value: Json) -> bool {
692    value.0.is_i64()
693}
694
695/// Determine if streamed JSON values are i64 numbers.
696#[mel_treatment(
697    input value Stream<Json>
698    output is_i64 Stream<bool>
699)]
700pub async fn is_i64() {
701    while let Ok(values) = value
702        .recv_many()
703        .await
704        .map(|values| Into::<VecDeque<Value>>::into(values))
705    {
706        check!(
707            is_i64
708                .send_many(TransmissionValue::Bool(
709                    values
710                        .into_iter()
711                        .map(|val| match val {
712                            Value::Data(val) => val
713                                .downcast_arc::<Json>()
714                                .map(|json| json.0.is_i64())
715                                .unwrap_or(false),
716                            _ => false,
717                        })
718                        .collect()
719                ))
720                .await
721        )
722    }
723}
724
725/// Tells if JSON value is a u64 number.
726#[mel_function]
727pub fn is_u64(value: Json) -> bool {
728    value.0.is_u64()
729}
730
731/// Determine if streamed JSON values are u64 numbers.
732#[mel_treatment(
733    input value Stream<Json>
734    output is_u64 Stream<bool>
735)]
736pub async fn is_u64() {
737    while let Ok(values) = value
738        .recv_many()
739        .await
740        .map(|values| Into::<VecDeque<Value>>::into(values))
741    {
742        check!(
743            is_u64
744                .send_many(TransmissionValue::Bool(
745                    values
746                        .into_iter()
747                        .map(|val| match val {
748                            Value::Data(val) => val
749                                .downcast_arc::<Json>()
750                                .map(|json| json.0.is_u64())
751                                .unwrap_or(false),
752                            _ => false,
753                        })
754                        .collect()
755                ))
756                .await
757        )
758    }
759}
760
761/// Tells if JSON value is a f64 number.
762#[mel_function]
763pub fn is_f64(value: Json) -> bool {
764    value.0.is_f64()
765}
766
767/// Determine if streamed JSON values are f64 numbers.
768#[mel_treatment(
769    input value Stream<Json>
770    output is_f64 Stream<bool>
771)]
772pub async fn is_f64() {
773    while let Ok(values) = value
774        .recv_many()
775        .await
776        .map(|values| Into::<VecDeque<Value>>::into(values))
777    {
778        check!(
779            is_f64
780                .send_many(TransmissionValue::Bool(
781                    values
782                        .into_iter()
783                        .map(|val| match val {
784                            Value::Data(val) => val
785                                .downcast_arc::<Json>()
786                                .map(|json| json.0.is_f64())
787                                .unwrap_or(false),
788                            _ => false,
789                        })
790                        .collect()
791                ))
792                .await
793        )
794    }
795}
796
797/// Tells if JSON value is a vector.
798#[mel_function]
799pub fn is_vec(value: Json) -> bool {
800    value.0.is_array()
801}
802
803/// Determine if streamed JSON values are vectors.
804#[mel_treatment(
805    input value Stream<Json>
806    output is_vector Stream<bool>
807)]
808pub async fn is_vector() {
809    while let Ok(values) = value
810        .recv_many()
811        .await
812        .map(|values| Into::<VecDeque<Value>>::into(values))
813    {
814        check!(
815            is_vector
816                .send_many(TransmissionValue::Bool(
817                    values
818                        .into_iter()
819                        .map(|val| match val {
820                            Value::Data(val) => val
821                                .downcast_arc::<Json>()
822                                .map(|json| json.0.is_array())
823                                .unwrap_or(false),
824                            _ => false,
825                        })
826                        .collect()
827                ))
828                .await
829        )
830    }
831}
832
833/// Tells if JSON value is an object.
834#[mel_function]
835pub fn is_object(value: Json) -> bool {
836    value.0.is_object()
837}
838
839/// Determine if streamed JSON values are objects.
840#[mel_treatment(
841    input value Stream<Json>
842    output is_object Stream<bool>
843)]
844pub async fn is_object() {
845    while let Ok(values) = value
846        .recv_many()
847        .await
848        .map(|values| Into::<VecDeque<Value>>::into(values))
849    {
850        check!(
851            is_object
852                .send_many(TransmissionValue::Bool(
853                    values
854                        .into_iter()
855                        .map(|val| match val {
856                            Value::Data(val) => val
857                                .downcast_arc::<Json>()
858                                .map(|json| json.0.is_object())
859                                .unwrap_or(false),
860                            _ => false,
861                        })
862                        .collect()
863                ))
864                .await
865        )
866    }
867}