diff --git a/src/emitter.c b/src/emitter.c
index f1e5f62e0f2b595ab5993eb873d85e50540f10b4_c3JjL2VtaXR0ZXIuYw==..dc53fae1805e077f618d199bed3e864bb0f3d36c_c3JjL2VtaXR0ZXIuYw== 100644
--- a/src/emitter.c
+++ b/src/emitter.c
@@ -1925,7 +1925,17 @@
 
     STRING_ASSIGN(string, value, length);
 
-    if (!emitter->whitespace) {
+    /**
+     * Avoid trailing spaces for empty values in block mode.
+     * In flow mode, we still want the space to prevent ambiguous things
+     * like {a:}.
+     * Currently, the emitter forbids any plain empty scalar in flow mode
+     * (e.g. it outputs {a: ''} instead), so emitter->flow_level will
+     * never be true here.
+     * But if the emitter is ever changed to allow emitting empty values,
+     * the check for flow_level is already here.
+     */
+    if (!emitter->whitespace && (length || emitter->flow_level)) {
         if (!PUT(emitter, ' ')) return 0;
     }